asp.net 删除网格视图行

nhaq1z21  于 2023-01-14  发布在  .NET
关注(0)|答案(2)|浏览(163)

我试图删除GridView中的一行,方法是输入type=“image”,该行的ID与图像关联,因此如果您单击该图像,它将调用www.example.com中的函数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>
wz1wpwve

wz1wpwve1#

命令按钮:

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

kgsdhlau

kgsdhlau2#

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

然后在代码后台使用网格视图的行删除

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();

        }
    }        
}

相关问题