How do I pass the Control ID in a gridview that is derived from a stored procedure into a variable. I will use the variable to pass into the database later on to return some results. Thanks.
Asked
Active
Viewed 1,177 times
2 Answers
1
var controlId = ((LinkButton)gridView1.Rows[0].FindControl("lbName")).Id;
Are you trying to do something like the above?
Update
You can use the OnSelectedIndex event of the GridView to find the row that was select.
void GridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
var controlId = ((LinkButton)row.FindControl("lbName")).Id;
}

Jethro
- 5,896
- 3
- 23
- 24
-
this wont work. you have to find the GridViewRow, then find the linkbutton inside the row – Jeff Jul 07 '11 at 17:47
-
@Jeff, oops, sorry forgot about that. Thanks. – Jethro Jul 07 '11 at 17:53
-
How would I write your scenario jeff? – jpavlov Jul 07 '11 at 17:53
-
@jpavlov, when are you trying to store the ControlId in a variable? – Jethro Jul 07 '11 at 17:55
-
Should I write my linkbutton for the asp.net like this? – jpavlov Jul 07 '11 at 17:59
-
– jpavlov Jul 07 '11 at 17:59
-
You've lost me, I thought you were trying to get the LinkButton Id from the GridView. – Jethro Jul 07 '11 at 18:06
-
@jpavlov let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1234/discussion-between-jethro-and-jpavlov) – Jethro Jul 07 '11 at 18:06
-
I have this in my PageLoad string controlId = (gv2.Rows[0].FindControl("lnkID")).ID; lblshow.Text = control(lnkID); – jpavlov Jul 07 '11 at 18:37
-
and this is in my .aspx page
-
But when i run it, it shows in the label not the actual ID but "lnkID. How should I return the value? – jpavlov Jul 07 '11 at 18:39
0
I tend to use the CommandName and CommandArgument fields with LinkButtons in a GridView.
The CommandName represents the name of the command, so that you can have more than one linkbutton that do different things and the CommandArgument is the Database ID or other point of reference for the command.
You need to subscribe to the RowCommand event handler on the GridView

Glenn Ferrie
- 10,290
- 3
- 42
- 73