Hi all,
I have a button which an on-click event and I am trying to get the current active grdiview on postback in order to select all the check-boxes on the gridview.
there are two GridViews on the page and only one will be visible or active. Is there a way to get the active one without having to set a value in a hidden field etc?
This is my code:
protected void btnSelect_Click(object sender, EventArgs e)
{
Page pg = (sender as Page);
GridView gv = pg.NamingContainer.Parent.Parent as GridView;
foreach (GridViewRow row in gv.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
(row.FindControl("cbSelect") as CheckBox).Checked = true;
}
}
}
I receive the error: Object reference not set to an instance of an object...on this line:
GridView gv = pg.NamingContainer.Parent.Parent as GridView;
I assume I cannot use the Page as the Naming container. So what should I use?
Thanks