简体   繁体   中英

How to retrieve the value in GridView when I click on LinkButton of GridView

I am using gridview to display of data from database, in each row of the gridview I am having delete and edit link button. How can I get value of “NAME” and "DESCRIPTION" when I click on Delete or Edit Button in gridview.

The following is my code.

List.aspx

        <div align="center" style="margin-top:50px">
          <asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" CellPadding="5" runat="server">
             <Columns>
                <asp:BoundField HeaderText="Report Name" DataField="NAME" />
                <asp:BoundField HeaderText="Report Description" DataField="DESCRIPTION" />
                <asp:BoundField HeaderText="Report Group" DataField="REPORT_GROUP" />
                <asp:TemplateField>
                <ItemTemplate>
                        <asp:LinkButton ID="btnDelete" Text="Delete" runat="server"  OnClick="Btn_Delete_Click" />
                        <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" />
                </ItemTemplate>
                </asp:TemplateField>
             </Columns>
             <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
             </asp:GridView>
        </div>   

List.aspx.cs

    protected void Btn_Delete_Click(object sender, EventArgs e)
    {

    }

use command argument like this

<ItemTemplate>
   <asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandArguman='<%# Eval("Name")+","+Eval("DESCRIPTION") %>' OnClick="Btn_Delete_Click" />
   <asp:LinkButton ID="btnEdit"  Text="Edit" runat="server" CommandArguman='<%# Eval("Name")+","+Eval("DESCRIPTION") %>' OnClick="btnEdit_Click" />
</ItemTemplate>

in onclick

protected void Btn_Delete_Click(object sender, EventArgs e)
    {
         string strName=((LinkButton)sender).CommandArgument.Split(',')[0];
         string strDescription=((LinkButton)sender).CommandArgument.Split(',')[1];
    }

protected void btnEdit_Click_Click(object sender, EventArgs e)
    {
         string strName=((LinkButton)sender).CommandArgument.Split(',')[0];
         string strDescription=((LinkButton)sender).CommandArgument.Split(',')[1];
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM