protected void viewHoursButton_OnClick(object sender, EventArgs e)
{
var viewHoursPopup = parentGridView.FindControl("viewHoursPopup")
as WebControl;
var viewHoursGridView = parentGridView.FindControl("viewHoursGridView");
if (viewHoursPopup != null && viewHoursGriView != null)
{
viewHoursPopup.Show();
viewHoursGridView.DataBind();
}
}
...or...
protected void viewHoursButton_OnClick(object sender, EventArgs e)
{
var viewHoursButton = (Button)sender;
var viewHoursPopup = viewHoursButton.Parent.FindControl("viewHoursPopup")
as WebControl;
var viewHoursGridView = viewHoursButton.Parent.FindControl("viewHoursGridView");
if (viewHoursPopup != null && viewHoursGriView != null)
{
viewHoursPopup.Show();
viewHoursGridView.DataBind();
}
}
Why?
ItemTemplates, RowTemplates, EditTemplates, etc, are all nested in what webforms calls NamingContainers. You cannot access these controls because they are injected dynamically at runtime, their control ID's are not automatically mapped by the codebehind's partial auto-generated class.
There are 2 solutions:
Wrap the content of your Template into a UserControl. That UserControl's codebehind will have compile-time access to any controls that are not further nested into other Templates. To enable interaction between the UserControl and the parent Template that contains it, expose events and public properties on the UserControl
Use .FindControl(string controlID) to "search" the containing control's current Templates to find the control you are looking for. Just be sure to check them against null before trying to invoke anything on them, and that they are cast to the appropriate type based on what methods you intend to invoke on them.