Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
How to retrieve a row from gridview (boundfields) when a link button is clicked(the linkbutton is in the itemtemplate )

Im having a hard time finding a clear answer for this question,
Can anyone please give an explanation or solution.
Links are also appreciated
Thanks

--I dont have google :)
Posted
Updated 28-Jan-14 15:47pm
v3

You can use GridView.RowCommand Event[^] for this.

So, declare it inside GridView.
ASP.NET
<asp:GridView ID="grdYourGridView" runat="server" OnRowCommand="GridViewCommandEventHandler" />

Now declare CommandName and CommandArgument properties inside LinkButton.
ASP.NET
<asp:LinkButton ID="lnkSomeLinkButton" 
                runat="server" 
                CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" 
                CommandName ="SomeCommandName">LinkButtonText</asp:LinkButton>

Now, in .cs page, write the Event Handler like...
VB
Sub ProductsGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

    ' If multiple buttons are used in a GridView control, use the
    ' CommandName property to determine which button was clicked.
    If e.CommandName = "SomeCommandName" Then

      ' Convert the row index stored in the CommandArgument
      ' property to an Integer.
      Dim index = Convert.ToInt32(e.CommandArgument)

      ' Retrieve the row that contains the button clicked 
      ' by the user from the Rows collection.
      Dim row = grdYourGridView.Rows(index)

      Dim someBoundFieldValue As String = row.Cells(CellIndex).Text

    End If

  End Sub
 
Share this answer
 
v4
Comments
Goenitz 29-Jan-14 0:50am    
Hi tadit, i have here a column which has the visibility = false, and when im calling the row.Cells(1).Text //im sure about the index// its returning a null value ("") what should i do now?
When you add Visible false, it don't render on page. So, you can't get the value.
Tom Marvolo Riddle 29-Jan-14 0:58am    
sorry Tadit :( .By mistake i voted 1 instead of 5.Now fixed
Oh, no problem. Thanks a lot Jas. :)
Instead of Visible false, you can do style="display:none;"
C#
GridViewRow clickedRow = ((LinkButton) sender).NamingContainer as GridViewRow;
    Label lblID = (Label)clickedRow.FindControl("lblID");
 
Share this answer
 
Attach a Click Event for the LinkButton which is placed in the ItemTemplate and access the Row like

protected void LinkButton1_Click(object sender, EventArgs e)
{
        LinkButton lb = (LinkButton)sender;
        GridViewRow row = (GridViewRow)lb.NamingContainer;
}
 
Share this answer
 
 
Share this answer
 
v2
Hi...
See this, in link button event (am updating gridview row fields)!
C#
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{    
// here gridview gv.     
    int id = Convert.ToInt32(gv.DataKeys[e.RowIndex].Values[0].ToString());
    string name = ((TextBox)gv.Rows[e.RowIndex].FindControl("txtname")).Text;
    string dstn = ((TextBox)gv.Rows[e.RowIndex].FindControl("txtdstn")).Text;
    string add = ((DropDownList)gv.Rows[e.RowIndex].FindControl("ddladd")).Text;
// like that retrieve values from gridview and do what ever u want...
//.....
}         

Thank u.
 
Share this answer
 
Use RowCommand Event for that.

Eg in .aspx page:
ASP.NET
<asp:templatefield headertext="Link" xmlns:asp="#unknown">
            <itemtemplate>
                <asp:linkbutton id="LinkButton1" runat="server">
CommandName ="Link">LinkButton</asp:linkbutton>
            </itemtemplate>
            </asp:templatefield>

In .CS page:C#
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
         if (e.CommandName == "Link")
            {
                LinkButton img = (LinkButton)e.CommandSource as LinkButton;
                GridViewRow row = img.NamingContainer as GridViewRow;

                Label lbid = (Label)row.FindControl("label1");//Label ID
                int  id = Convert.ToInt32(lbid.Text);

            }
     }

VB:
VB
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
    If e.CommandName = "Link" Then
        Dim img As LinkButton = TryCast(DirectCast(e.CommandSource, LinkButton), LinkButton)
        Dim row As GridViewRow = TryCast(img.NamingContainer, GridViewRow)

        Dim lbid As Label = DirectCast(row.FindControl("label1"), Label)
        'Label ID

        Dim id As Integer = Convert.ToInt32(lbid.Text)
    End If
End Sub
 
Share this answer
 
v2
Comments
Tom Marvolo Riddle 28-Jan-14 23:52pm    
I don't know VB.i used online conversion so try it and let me know
try this:-

XML
<asp:TemplateField HeaderText="Link">
<ItemTemplate>
<asp:ImageButton ID="imgbtnLinked" CommandName="Link" CommandArgument='<%# Eval("Bank_TrxID") %>'runat="server" ImageUrl="~/Images/linkbtn.png" ToolTip="Link" Width="12px" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>


in backend (change your code as per your requirement):-

VB
Protected Sub Gridview1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles Gridview1.RowCommand
Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, Control).NamingContainer, GridViewRow)
Dim str As String = row.Cells(1).Text   //Change your cell index.
End Sub


hope it hepls.
 
Share this answer
 
v3
Comments
TrushnaK 29-Jan-14 0:24am    
why it is downvoted... any reason.
Goenitz 29-Jan-14 0:31am    
My columns are boundfields not label :)
Tom Marvolo Riddle 29-Jan-14 1:02am    
@Goenitz I too did the same mistake.sorry for that
Goenitz 29-Jan-14 1:15am    
Dont worry about it :)
TrushnaK 29-Jan-14 2:28am    
you want exact answers..
don't you want to take any efforts yourself..

Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, Control).NamingContainer, GridViewRow)
Dim str As String = row.Cells(1).Text

this two lines solves your problem..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900