【问题标题】:Delete GridView Row删除 GridView 行
【发布时间】:2012-02-29 17:52:01
【问题描述】:

我试图通过输入 type="image" 和该行的 ID 与图像关联来删除 GridView 中的一行,因此如果您单击该图像,它将调用 asp.net 中的一个函数并删除根据该 ID 从数据库中记录并重新加载该网格。 解决这个问题的最佳方法是什么?感谢您的帮助。

<asp:GridView ID="grdHouses" runat="server"
        CssClass="gridview"
        RowStyle-CssClass="gridview_itm"
        AlternatingRowStyle-CssClass="gridview_aitm"
        HeaderStyle-CssClass="gridview_hdr" Width="100%" AutoGenerateColumns="False">
  <Columns>
    <asp:TemplateField HeaderText="" ItemStyle-Width="2%">
      <ItemTemplate>
        <input type="image" name="imgDelete" class="listViewTdToolsS1" src="../App_Themes/Sugar2006/images/delete_inline.gif" alt="rem" style="height:12px;width:12px;border-width:0px;" value=""
        <%# DataBinder.Eval(Container.DataItem, "ID") %>">
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
      <HeaderTemplate>
        <asp:LinkButton ID="Name_SortLnkBtn" runat="server" Text="NAME" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Name" CausesValidation="false" />
        <asp:ImageButton ID="Name_SortImgBtn" runat="server" Visible="false" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Name" CausesValidation="false" />
      </HeaderTemplate>
      <ItemTemplate>
        <%#Eval("Name")%>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
      <HeaderTemplate>
        <asp:LinkButton ID="Description_SortLnkBtn" runat="server" Text="DESCRIPTION" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Description" CausesValidation="false" />
        <asp:ImageButton ID="Description_SortImgBtn" runat="server" Visible="false" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Description" CausesValidation="false" />
      </HeaderTemplate>
      <ItemTemplate>
        <%#Eval("Description")%>
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

【问题讨论】:

  • 在gridview中添加d​​ataKeyNames="Id"

标签: asp.net gridview


【解决方案1】:

命令按钮:

<asp:TemplateField HeaderText="" ItemStyle-Width="2%">
 <ItemTemplate>        
         <asp:ImageButton runat="server"  
             CommandName='DeleteItem'
             CommandArgument = '<%# Eval("ID") %>'
             CssClass="listViewTdToolsS1"
             ImageUrl="~/App_Themes/Sugar2006/images/delete_inline.gif"
             Tooltip="rem"
 </ItemTemplate>     
</asp>

处理 GridView OnItemCommand 事件:

void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="DeleteItem")
    {
         var id = Int32.Parse(e.CommandArgument);
         // delete the object from database
         Database.DeleteObject(id);
         // rebind the gridview
         grdHouses.DataSource = DataBase.GetItems();
          grdHouses.DataBind();
    }
  }

由于 DataSource 是在后面的代码中设置的,我建议使用 DeleteItem 或其他代替 Delete 作为命令名称。

【讨论】:

  • 嘿,感谢您的帮助,但现在我收到一个错误:无效的回发或回调参数。使用配置中的 或页面中的 启用事件验证。出于安全目的,此功能验证回发或回调事件的参数是否源自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法注册回发或回调数据以进行验证。
  • 我改变了一点我的答案,也许是这样。您不再需要以下内容
【解决方案2】:

首先像这样在您的网格视图中添加 DataKeyName

<asp:GridView ID="grdHouses" runat="server"
    CssClass="gridview" DataKeyNames="ID"
    RowStyle-CssClass="gridview_itm"
    AlternatingRowStyle-CssClass="gridview_aitm"
    HeaderStyle-CssClass="gridview_hdr" Width="100%" AutoGenerateColumns="False" onrowdeleting="grdHouses_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="ID">
        <ItemTemplate>
           <asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
        </ItemTemplate>
     </asp:TemplateField>
<asp:TemplateField>
  <HeaderTemplate>
    <asp:LinkButton ID="Name_SortLnkBtn" runat="server" Text="NAME" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Name" CausesValidation="false" />
    <asp:ImageButton ID="Name_SortImgBtn" runat="server" Visible="false" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Name" CausesValidation="false" />
  </HeaderTemplate>
  <ItemTemplate>
    <%#Eval("Name")%>
  </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
  <HeaderTemplate>
    <asp:LinkButton ID="Description_SortLnkBtn" runat="server" Text="DESCRIPTION" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Description" CausesValidation="false" />
    <asp:ImageButton ID="Description_SortImgBtn" runat="server" Visible="false" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Description" CausesValidation="false" />
  </HeaderTemplate>
  <ItemTemplate>
    <%#Eval("Description")%>
  </ItemTemplate>
</asp:TemplateField>
 <asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
    <span onclick="return confirm('Are you sure to Delete the record?')">
       <asp:LinkButton ID="lnkB" runat="Server" Text="Delete" CommandName="Delete">    </asp:LinkButton>
     </span>
 </ItemTemplate>

 </asp:TemplateField>
  </Columns>
 </asp:GridView>

然后在代码后面使用gridview的RowDeleting

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    try
    {
        string id = GridView1.DataKeys[e.RowIndex].Values["ID"].ToString();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Delete FROM Table Name where ID='" + id + "'";
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();                              
    }
    catch (Exception ex)
    {

    }
    finally
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();

        }
    }        
}

【讨论】:

    猜你喜欢
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多