Link to home
Start Free TrialLog in
Avatar of Tammu
Tammu

asked on

linkbutton in a GridView

Hi Experts,
this might be very easy. i have a linkbutton inside a gridview. so basically every row will have this link button. what i am trying to do is to click this button and it should display the button text in a label. very simple. but right now if i click the button in any row of the gridview, it only display the last row button text.
i know i am making a mistake but dont know where. what i want is to display the text of the button for whatever row i clicked. this is what i mean

LinkButton1 --------- inside a Gridview
-----------
A12
A13
A14
A15
so if i click on A12, it should display A12 in the label and similarly for A13, A14,A15 but right now it only display A15 no matter where i click

here is the code for the linkbutton
 protected void LinkButton1_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
          {
              LinkButton lbBtn = (LinkButton)row.FindControl("LinkButton1");
              lbtest.Text = lbBtn.Text;
            }
         
        }
any help is appreciated.
Thanks
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

In row command if you want to get the current row or its index, It is not directly possible. There is two way, one is on row created event on the link button(suppose, it is used for row command) save row index in link button command argument attribute.

And get the argument from row command event using e.CommandArgument

But if you want to send another value as a command argument then it creates problem. To solve this just use this code

GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

Here link button (or any source) that cause to enter in row command event. Now you have selected row so you dont need any index and directly access any row variable as sample code is given below
Label lblProdId = (Label)row.FindControl(lblproductId);

So whole code is just two line. Below I write whole code

GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

Label lblProdId = (Label)row.FindControl(lblproductId);

Source: http://ranafaisal.wordpress.com/2008/03/31/how-to-get-the-current-row-in-gridview-row-command-event/
Avatar of Tammu
Tammu

ASKER

Hi Sir,
this is how i did but its not working getting an error

protected void LinkButton1_Click(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

// finding the linkbutton
LinkButton lbBtn = (LinkButton)row.FindControl("LinkButton1");

// displaying the linkbutton text in a label
lbtest.Text = lbBtn.Text;
}
what am i doing wrong sir
Thanks
As I can remember in this case when you say NamingContrainer it gives you the GridView Cell not the row. So you have to do it like,

GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer.NamingContainer);

I'm not quite sure and I can't test this for you now. Just debug and see how this comes.
Get rid of the whole "foreach" loop and use this two lines of code:
LinkButton lbtn = (LinkButton)sender;
lbtest.Text = lbBtn.Text;
 
ASKER CERTIFIED SOLUTION
Avatar of prairiedog
prairiedog
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial