删除GridView行

时间:2012-02-29 17:52:02

标签: asp.net gridview

我试图通过输入类型=“图像”和与该图像关联的该行的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>

2 个答案:

答案 0 :(得分:0)

命令按钮:

<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 作为命令名称。

答案 1 :(得分:0)

首先在网格视图中添加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();

        }
    }        
}