Sometime when we delete the data with GridView,there needs a confirm dialog to get the user's operate.if confirm to delete,let gridview delete the right data.else not.
HTML页面中代码:
<asp:GridView DataKeyNames="CategoryID" ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("CategoryID") %>' CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
上 面代码中设置了linkbutton,指定了commandname为"Delete",commandargument为要删除的记录的ID编号,一旦 commandname设置为delete这后,gridview中的GridView_RowCommand 和 GridView_Row_Deleting 事件都会被激发,处理其rowdatabound事件中:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
l.Attributes.Add("onclick", "javascript:return " + "confirm('是否要删除该记录? " +
DataBinder.Eval(e.Row.DataItem, "id") + "')");
}
}
首先检查是否是datarow,是则得到每个linkbutton,再为其添加客户端代码。
当用户选择了确认删除后,在row_command事件中写代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int id = Convert.ToInt32(e.CommandArgument);
// 删除记录的专门过程
DeleteRecordByID(id);
}
}
if you wanna some special button have the same effect.you can try this method:
btnSpecial.Attributes.Add("onclick","javascript:return confirm('确认删除吗?');");
HTML页面中代码:
<asp:GridView DataKeyNames="CategoryID" ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("CategoryID") %>' CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
上 面代码中设置了linkbutton,指定了commandname为"Delete",commandargument为要删除的记录的ID编号,一旦 commandname设置为delete这后,gridview中的GridView_RowCommand 和 GridView_Row_Deleting 事件都会被激发,处理其rowdatabound事件中:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
l.Attributes.Add("onclick", "javascript:return " + "confirm('是否要删除该记录? " +
DataBinder.Eval(e.Row.DataItem, "id") + "')");
}
}
首先检查是否是datarow,是则得到每个linkbutton,再为其添加客户端代码。
当用户选择了确认删除后,在row_command事件中写代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int id = Convert.ToInt32(e.CommandArgument);
// 删除记录的专门过程
DeleteRecordByID(id);
}
}
if you wanna some special button have the same effect.you can try this method:
btnSpecial.Attributes.Add("onclick","javascript:return confirm('确认删除吗?');");