I am dynamically creating several gridviews depending on data in a DB.
I generate the gridview as follows in a foreach statement:
GridView gdv = new GridView();
gdv.ID = "gdv" + i.ToString();
gdv.SelectedIndexChanged += new EventHandler(gdv_SelectedIndexChanged);
gdv.RowDataBound += gdv_RowDataBound;
RowDataBound looks like the following:
protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(((GridView)sender), "Select$" + e.Row.RowIndex);
}
}
This all works as expected, however the ((GridView)sender) returns the following value:
'gdv00'
What i need returned for this link to work is the following:
'ctl00$ctl00$MainContent$Main$gdv0'
Pretty sure this has something to do with the master pages? but not sure how to reference this properly in the rowdatabound event?
The ID of ctl00$ctl00$MainContent$Main$gdv is the UniqueID property of the grid control (client ID is separated by _). This is the ID rendered to the browser. I think the problem here is that the control isn’t added to the control tree immediately, which has been a problem for some in the past. Try doing it this way:
HTH.