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
LVL 2
niceoneishereAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DhaestCommented:
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/
0
niceoneishereAuthor Commented:
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
0
jandromedaCommented:
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.
0
prairiedogCommented:
Get rid of the whole "foreach" loop and use this two lines of code:
LinkButton lbtn = (LinkButton)sender;
lbtest.Text = lbBtn.Text;
 
0
prairiedogCommented:
Sorry, typo. Here you go:
LinkButton lblBtn = (LinkButton)sender;
lbtest.Text = lbBtn.Text;

 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.