65.9K
CodeProject 正在变化。 阅读更多。
Home

GridView 删除,带确认

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.73/5 (67投票s)

2006 年 1 月 10 日

3分钟阅读

viewsIcon

956951

downloadIcon

10331

如何在 GridView 中实现行删除,并带有确认提示。

引言

每个人都喜欢确认,让他们知道正在删除一条记录。 在本文中,我将向您展示如何在从 GridView 控件中删除记录时提示确认框。

实现确认功能

您需要做的第一件事是将 JavaScript 确认代码附加到 GridView 控件的删除列。 这可以在 GridView 控件的 Row_DataBound 事件中完成。 Row_DataBound 事件在将行附加到 GridView 时触发。 因此,在首次构建 GridView 或重新加载页面时都会触发此事件。

让我们看一下 GridView 代码的 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>

从上面的代码中可以看出,我在 GridView 中有三列。 列 CategoryIDCategoryName 是绑定列,列 Delete 是一个模板列。 命令参数设置为 CategoryID,这意味着每当单击 LinkButton 时,它都会将 CategoryID 作为参数传递。 CommandName 设置为 "Delete"。

CommandName 属性非常重要。 如果您在 GridView 控件的模板列内有一个 LinkButtonButton 控件,并且 CommandName 属性设置为“Delete”,那么除了 GridView_RowCommand 事件之外,还会触发 GridView_Row_Deleting 事件。

现在,让我们看一下将 JavaScript 代码附加到每个 LinkButtonGridView_RowBound 事件。

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('Are you sure you want to delete this record " +
    DataBinder.Eval(e.Row.DataItem, "CategoryID") + "')"); 
  }
}

在上面的代码中,我检查了 GridView 行是否为 DataRow,如果是,我只需使用 Attributes.Add 方法附加一些 JavaScript 代码。

捕获被单击行的主键

现在您已经成功地将 JavaScript 代码附加到 GridView 控件,剩下的就是捕获您已单击的行的主键,以便您可以执行进一步的操作(例如删除该行)。 记住我之前说的关于 LinkButtonButton 控件,其 CommandName 设置为“Delete”? 如果您不记得了,请再次阅读框中的文本。

CommandName 属性非常重要。 如果您在 GridView 控件的模板列内有一个 LinkButtonButton 控件,并且 CommandName 属性设置为“Delete”,那么除了 GridView_RowCommand 事件之外,还会触发 GridView_Row_Deleting 事件。

现在,由于我们的 LinkButtonCommandName 设置为“Delete”,这意味着我们有两个选择可以从 GridView 获取主键。 我们可以在 RowCommand 事件中执行此操作,也可以在 Row_Deleting 事件中执行此操作。 我将向您展示这两个方法。

在 RowCommand 事件中捕获主键

这非常简单。 您所需要做的就是从您已经设置为 CategoryIDCommandArgument 属性中获取值。

protected void GridView1_RowCommand(object sender, 
                         GridViewCommandEventArgs e)
{
  if (e.CommandName == "Delete")
  {
    // get the categoryID of the clicked row
    int categoryID = Convert.ToInt32(e.CommandArgument);
    // Delete the record 
    DeleteRecordByID(categoryID);
    // Implement this on your own :) 
  }
}

e.CommandArgument 返回 object,因此您需要将其转换为 int,就像我上面做的那样。

在 Row_Deleting 事件中捕获主键

让我们看看如何在 Row_Deleting 事件中捕获被单击行的主键。

<asp:GridView DataKeyNames="CategoryID" ID="GridView1" 
     runat="server" AutoGenerateColumns="False" 
     OnRowCommand="GridView1_RowCommand" 
     OnRowDataBound="GridView1_RowDataBound"
     OnRowDeleted="GridView1_RowDeleted" 
     OnRowDeleting="GridView1_RowDeleting">
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
  int categoryID = (int) GridView1.DataKeys[e.RowIndex].Value;
  DeleteRecordByID(categoryID); 
}

在这种技术中,您必须将 GridViewDataKeyNames 属性设置为 "CategoryID"。 GridView1.DataKeys[e.RowIndex].Value 属性从被单击的行中获取 CategoryID。 我已将源代码文件附带在此项目中,因此请随意下载它们。

希望您喜欢这篇文章,祝您编码愉快!

© . All rights reserved.