Access Control From GridView RowCommand
Posted on: Tue Nov 11 15:44:17 -0800 2008. Updated on: Tue Nov 11 15:55:52 -0800 2008.
Category: DotNet
Category: DotNet
Let's say you have a GridView and you have the RowCommand defined to a function. We often want to get access to the controls in the Row so as to see what values the user selected. Here is an example:
protected void GridView1Items_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ViewList")
{
// Here is the trick to get access to the Row.
GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
// Now we can access a dropdown for example and get the selected value.
DropDownList ddLists = (DropDownList)row.FindControl("DropDownLists");
int listId = Convert.ToInt32(ddLists.SelectedValue);
PreviewList1.ListId = listId;
}
This comes in handy almost any time you want to override the GridView's default behavior for editing / inserting data.