VS2005,asp.net:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)(e.Row.Cells[1].Controls[0])).Attributes.Add("onclick", "javascript:return confirm('确认删除这条信息吗?');");
}
}
前边一些关于删除的数据库命令,都好说,关键是这一步,当单击删除的按钮时,触发的事件
我的删除按钮放在GridView的第二个位置上,而且只有一个按钮,所以Cell[1].Control[0],
但是一运行,就报错,说超出了有效值范围。很郁闷
望大侠指教
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)(e.Row.Cells[1].Controls[0])).Attributes.Add("onclick", "javascript:return confirm('确认删除这条信息吗?');");
}
}
前边一些关于删除的数据库命令,都好说,关键是这一步,当单击删除的按钮时,触发的事件
我的删除按钮放在GridView的第二个位置上,而且只有一个按钮,所以Cell[1].Control[0],
但是一运行,就报错,说超出了有效值范围。很郁闷
望大侠指教
lb.Attributes.Add("onclick", "javascript:return confirm('确认删除这条信息吗?');");
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from tb_Member where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcon = new SqlConnection(strCon);
sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
bind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)(e.Row.Cells[4].Controls[0])).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
}
}
给删除的deteletext加<div id="de" onclick="JavaScript:return confirm('确定删除吗?')">删除</div>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string proId = e.CommandArgument.ToString();
if(e.CommandName=="Delete")
{
ProductManager pm = new ProductManager();
pm.DeleteProduct(proId);
Response.Redirect("list.aspx");
}
FillView();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
LinkButton lb = e.Row.FindControl("LinkButton1") as LinkButton;
lb.Attributes.Add("onclick", "return confirm('确认删除?')");
}
}试试吧
你不妨把脚本写在前台
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
}
}
{
if ((e.Row.RowState & DataControlRowState.Edit) != DataControlRowState.Edit)//不为编辑状态
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.Cells[13].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('确认要删除吗?')");//删除前弹出是否确定的对话框
}
}
}这个肯定可以了
试试~
e.Row.Cells[1].Controls[0]改为e.Row.Cells[1].Controls[1]这样可以解决。
for (int i = 0; i < GridView1.Rows.Count; i++)
{
((LinkButton)(GridView1.Rows[i].Cells[6].Controls[0])).Attributes.Add("onClick", "javascript:if(confirm('您真的要删除选中的记录吗?')!=1){return false}");
}
是不是Rows[i]的原因。
Text="删除"></asp:LinkButton>