【问题标题】:delete data with linkbutton in gridview in asp.net在asp.net的gridview中使用linkbutton删除数据
【发布时间】:2015-11-26 13:04:32
【问题描述】:

我正在对批量数据使用网格视图,数据显示完美,但我希望删除按钮可用,即每一行中的删除按钮,如下所示

为此我正在使用 MySqlDataAdapter adp = web_pages.start_page.database.getCriminalsByName(n); DataTable dt = new DataTable(); adp.Fill(dt); if (dt.Rows.Count > 0) { GridView1.DataSource = dt; GridView1.DataBind(); }

HTML 代码:

<asp:GridView ID="GridView1" runat="server" style="margin-top: 27px; color: #FFFFFF;" Width="502px">
            <Columns>
                <asp:ButtonField CommandName="Delete" Text="Delete" ButtonType="Button">
                <ControlStyle BackColor="Transparent" BorderColor="Transparent" ForeColor="White" />
                <FooterStyle BackColor="Transparent" />
                <HeaderStyle BackColor="Transparent" />
                <ItemStyle BackColor="#242529" BorderWidth="0px" />
                </asp:ButtonField>
                <asp:CommandField ShowEditButton="True">
                <ControlStyle ForeColor="White" />
                </asp:CommandField>
                <asp:ButtonField ButtonType="Button" CommandName="Select" HeaderText="Victim Details" ShowHeader="True" Text="View Victims">
                <ControlStyle BackColor="Transparent" BorderColor="Transparent" ForeColor="White" />
                </asp:ButtonField>
            </Columns>
        </asp:GridView>

所以任何人都可以帮助找到该行的索引或任何其他方法。

【问题讨论】:

  • 发布你的gridview的html标记。

标签: c# asp.net gridview


【解决方案1】:

可以使用Gridview的RowCommand事件

在网格视图中

 <asp:TemplateField HeaderText="Delete">
      <ItemTemplate>
          <asp:LinkButton ID="lnkDelete" runat="server" CommandName="dede" CommandArgument='<%#Eval("ID") %>'>Delete</asp:LinkButton>
</ItemTemplate>
     </asp:TemplateField>

在 CS 页面中可以调用 RowCommand 事件

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

            if (e.CommandName == "dede")
            {

                int i = 0;
//BLL.DBClass is my Custom Connection Class
                i = BLL.DBClass.ExecNonQueryInt("delete from table where id=" + Convert.ToInt32(e.CommandArgument) + "", null, false);



            }

           }

【讨论】:

  • Convert.ToInt32(e.CommandArgument) 说错误,即输入字符串的格式不正确。
  • CommandArgument=''> 在 Eval 中你传递了什么值,请告诉我你写的查询
【解决方案2】:

一个小错误只需替换 CommandArgument='' ,因为 Akhil 给出的解决方案中还有额外的 '>'。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    相关资源
    最近更新 更多