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.FindContro l("LinkBut ton1");
lbtest.Text = lbBtn.Text;
}
}
any help is appreciated.
Thanks
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.FindContro
lbtest.Text = lbBtn.Text;
}
}
any help is appreciated.
Thanks
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.Command Source).Na mingContai ner);
// finding the linkbutton
LinkButton lbBtn = (LinkButton)row.FindContro l("LinkBut ton1");
// displaying the linkbutton text in a label
lbtest.Text = lbBtn.Text;
}
what am i doing wrong sir
Thanks
this is how i did but its not working getting an error
protected void LinkButton1_Click(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)(((LinkButton
// finding the linkbutton
LinkButton lbBtn = (LinkButton)row.FindContro
// 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.Command Source).Na mingContai ner.Naming Container) ;
I'm not quite sure and I can't test this for you now. Just debug and see how this comes.
GridViewRow row = (GridViewRow)(((LinkButton
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;
LinkButton lbtn = (LinkButton)sender;
lbtest.Text = lbBtn.Text;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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/