Findcontrols inside content pages
I'm using the following code in my master page开发者_如何学JAVA to find controls inside content pages. On the first page load, it works fine; but when i select an item from a dropdown which is on content page and it autopostback is enabled, then i'm getting the error "object reference not set to an instance of a object". This means the FindControl() function was unable to find the controls inside content pages. What is the reason for this issue?
PlaceHolder pHolder = (PlaceHolder)ContentPlaceHolder2.FindControl("PlaceHolder1");
Label lblPage = (Label)pHolder.FindControl("lblPageName");
if (lblPage.Text == "DesignSerachSQGrid")
{
}
edit...
Html code for the dropdown:
<telerik:RadComboBox ID="ddlSearchL1" runat="server" Width="400px" Skin="Office2007"
onselectedindexchanged="ddlSearchL1_SelectedIndexChanged"
AutoPostBack="True" Font-Names="Eras Medium ITC" Font-Size="X-Small"
onprerender="ddlSearchL1_PreRender">
<Items>
<telerik:RadComboBoxItem runat="server" Text="Please Select ................"
Value="Please Select ................" Font-Name="Eras Medium ITC" />
<telerik:RadComboBoxItem runat="server" Text="Design Documents"
Value="Design Documents" Font-Name="Eras Medium ITC" />
<telerik:RadComboBoxItem runat="server" Text="Vendor Documents"
Value="Vendor Documents" Font-Name="Eras Medium ITC" />
<telerik:RadComboBoxItem runat="server" Text="Business Documents"
Value="Business Documents" Font-Name="Eras Medium ITC" />
<telerik:RadComboBoxItem runat="server" Text="Management Documents"
Value="Management Documents" Font-Name="Eras Medium ITC" />
<telerik:RadComboBoxItem runat="server" Text="Squad Check - Management"
Value="Squad Check - Management" Font-Name="Eras Medium ITC" />
<telerik:RadComboBoxItem runat="server" Text="Transmittal - Management"
Value="Transmittal - Management" Font-Name="Eras Medium ITC" />
<telerik:RadComboBoxItem runat="server" Text="Transmittal - Design"
Value="Transmittal - Design" Font-Name="Eras Medium ITC" />
<telerik:RadComboBoxItem runat="server" Text="Squad Check - Design"
Value="Squad Check - Design" Font-Name="Eras Medium ITC" />
<telerik:RadComboBoxItem runat="server" Text="Transmittal - Vendor"
Value="Transmittal - Vendor" Font-Name="Eras Medium ITC" />
<telerik:RadComboBoxItem runat="server" Text="Squad Check - Vendor"
Value="Squad Check - Vendor" Font-Name="Eras Medium ITC" />
</Items>
</telerik:RadComboBox>
You can check this post : ASP.NET 2.0 MasterPages and FindControl()
/// <summary>
/// Finds a Control recursively. Note finds the first match and exists
/// </summary>
/// <param name="ContainerCtl"></param>
/// <param name="IdToFind"></param>
/// <returns></returns>
public static Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
use it
this.dgItemList = FindControlRecursive(this.Master, "dgItemList") as DataGrid;
精彩评论