1

I have a gridview that has rows with an asp:Button and some boundfields as ID, Date, Subject, Username etc. I need to get ID value of an item by using Show button in that row. How can I do it in codebehind?

My ASPX codes:

<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="BtnGetId" runat="server" Text="Show" OnClick="BtnGetId_Click" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

And my C# codes should be like this:

protected void BtnGetId_Click(object sender, EventArgs e)
{
    // GETS ROW'S ID VALUE
}

Thanks for help.

smtnkc
  • 488
  • 2
  • 9
  • 23
  • 1
    Why not use `RowCommand Event` ? – Suraj Singh Dec 10 '13 at 11:10
  • @sametenekeci Then you should read about `events` in GridView, Follow these links they will help you , for more links i will add up in answer as it will become messy here in comments. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand(v=vs.110).aspx – Suraj Singh Dec 10 '13 at 11:21
  • 1
    @sametenekeci Go through this tutorial also http://www.c-sharpcorner.com/uploadfile/syedshakeer/rowcommand-event-in-gridview/ – Suraj Singh Dec 10 '13 at 11:28
  • @SurajSingh Thanks a lot! I got it through this tutorial. – smtnkc Dec 10 '13 at 11:48

2 Answers2

0

You will have to use:

ForEach(GridViewRow  row In GrdJobDetails.Rows)
{
  variable=(control)(row.findcontrol("controlID"),control).Property
}

Above thing i have written syntactically. You will have to mold it according to your case.

C Sharper
  • 8,284
  • 26
  • 88
  • 151
0
<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="BtnGetId" runat="server" Text="Show" CommandArgument='<%#Eval("ID")%>' CommandName="btnClick" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "btnClick")
    {
       int ID = Convert.toInt32(e.CommandArgument);
    }
}
Paulo Correia
  • 616
  • 5
  • 15
Suraj Singh
  • 4,041
  • 1
  • 21
  • 36