I can't seem to be able to find a nested control on my ASP.NET Page.
The ClientID of the control is the following: "mainContent_Sites_TabContainer_Panel_0_ctl00_Configs_TabContainer_Panel_1_ctl00_Lvl1_7_Lvl2_8_Lvl3_9_Lvl4_45"
As you can see it's nested in 2 tabContainers and then in a hierarchy of other custom controls I've written.
The control I'm interested in is "Lvl4_45".
FYI: Below is my page markup. The first TabContainer is named "Sites_TabContainer", this gets custom content which in turn has another tabcontainer which in turn has a hierarchy of 4 nested custom controls.:
For example, if I want to find a control, which has a ID "customLabelControl" in my aspx page, in code behind
var masterPage = Page.Master;
if (masterPage != null)
{
var findLabelControl = (CustomLabelControl) masterPage.FindControl(ctl00$MainContent$customLabelControl); //Cast to my custom control findLabelControl.Text = "I found my Custom Label Control"; }
Please "Mark as Answer" if this post answered your question.
Yannick86
Member
565 Points
366 Posts
Can't find nested control on page
Mar 29, 2012 07:54 AM|LINK
Hey guys,
I can't seem to be able to find a nested control on my ASP.NET Page.
The ClientID of the control is the following: "mainContent_Sites_TabContainer_Panel_0_ctl00_Configs_TabContainer_Panel_1_ctl00_Lvl1_7_Lvl2_8_Lvl3_9_Lvl4_45"
As you can see it's nested in 2 tabContainers and then in a hierarchy of other custom controls I've written.
The control I'm interested in is "Lvl4_45".
I've tried to fetch the control with the FindControlRecursive method in this tutorial, which I've successfully used in other scenario's:
http://www.asp.net/web-forms/tutorials/master-pages/control-id-naming-in-content-pages-cs
Still I'm unable to retrieve the control.
Anybody got an idea?
Thanks in advance,
- Yannick
FYI: Below is my page markup. The first TabContainer is named "Sites_TabContainer", this gets custom content which in turn has another tabcontainer which in turn has a hierarchy of 4 nested custom controls.:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Version.aspx.cs" Inherits="OT.Pages.Users.Version"%> <%@ Register Assembly="OT" Namespace="CustomControls" TagPrefix="cc1" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> <script src="../../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script> <script src="../../../Scripts/jquery-ui-1.8.18.custom.min.js" type="text/javascript"></script> <script type="text/javascript"> function CreateConfigButtonState(TitleField, SaveButton, UnitField) { //alert($(sender).val()); //alert($(UnitField).get(0).selectedIndex); if ($(TitleField).val() == "" || $(UnitField).get(0).selectedIndex == 0) { $(SaveButton).attr("disabled", true); } else { $(SaveButton).removeAttr("disabled"); } } </script> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="sideContent" runat="server"> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="mainContent" runat="server"> <asp:Panel ID="Panel1" runat="server" Width="100%"> <asp:Table runat="server" Width="99.9%"> <asp:TableRow> <asp:TableCell CssClass="OTPanel" Width="100%"> <h2> <table style="height:24px" width="100%"> <tr> <td> Version details </td> <td align="right"> <asp:ImageButton runat="server" ImageUrl="~/Content/Images/Attachment_24.png" ID="Btn_ManageAttachments" OnClick="Btn_ManageAttachments_OnClick" ToolTip="Manage attachments" /> <asp:ImageButton runat="server" ImageUrl="~/Content/Images/Comments_24.png" ID="Btn_EditDescription" OnClick="Btn_EditDescription_OnClick" ToolTip="Edit description" /> <asp:ImageButton runat="server" ID="Btn_GoToVersionHierarchy" OnClick="Btn_GoToVersionHierarchy_OnClick" ImageUrl="~/Content/Images/Hierarchy32.png" ToolTip="Edit quote hierarchy" Width="24px" Height="24px" /> <asp:ImageButton runat="server" ID="Btn_SaveVersion" ImageUrl="~/Content/Images/Save_24.png" ToolTip="Save version" /> </td> </tr> </table> </h2> <asp:Table ID="Table2" runat="server"> <asp:TableRow> <asp:TableCell VerticalAlign="Top"> <asp:Label runat="server" ID="Lbl_Version_Details_Firm_Lbl" Text="Firm:" Font-Bold="true" /> </asp:TableCell> <asp:TableCell> <asp:Label runat="server" ID="Lbl_Version_Details_Firm" /> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell VerticalAlign="Top"> <asp:Label runat="server" ID="Lbl_Version_Details_Contact_Lbl" Text="Contact:" Font-Bold="true" /> </asp:TableCell> <asp:TableCell> <asp:Label runat="server" ID="Lbl_Version_Details_Contact" /> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell VerticalAlign="Top"> <asp:Label runat="server" ID="Lbl_Version_Details_Description_Lbl" Text="Description:" Font-Bold="true" /> </asp:TableCell> <asp:TableCell> <asp:Literal runat="server" ID="Txt_Version_Details_Description" /> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell VerticalAlign="Top"> <asp:Label runat="server" ID="Lbl_Version_Details_CreationDate_Lbl" Text="Created:" Font-Bold="true" /> </asp:TableCell> <asp:TableCell> <asp:Label runat="server" ID="Lbl_Version_Details_CreationDate" /> </asp:TableCell> </asp:TableRow> </asp:Table> </asp:TableCell> <asp:TableCell CssClass="OTPanel" VerticalAlign="Top"> <h2> <table width="612px" style="height:24px"> <tr> <td> Add new site </td> <td align="right"> <div style="height:26px"/> </td> </tr> </table> </h2> <asp:Table ID="Table1" runat="server"> <asp:TableRow> <asp:TableCell> <asp:Label runat="server" ID="Label3" Text="Site:" Font-Bold="true" /> </asp:TableCell> <asp:TableCell> <asp:TextBox ID="Filter_Customer" runat="server" AutoPostBack="true" autocomplete="off" Width="500" OnTextChanged="Filter_Customer_OnTextChanged" /> <asp:HiddenField runat="server" ID="Filter_Customer_AutocompleteValue" OnValueChanged="Filter_Customer_AutocompleteValue_OnValueChanged" /> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell> <asp:Button runat="server" ID="Btn_AddSiteTab" OnClick="Btn_AddSiteTab_OnClick" Text="+" Enabled="false" /> </asp:TableCell> <asp:TableCell> <asp:Label runat="server" ID="Lbl_AddSiteTab" /> </asp:TableCell> </asp:TableRow> </asp:Table> </asp:TableCell> </asp:TableRow> </asp:Table> <ajaxToolkit:AutoCompleteExtender runat="server" UseContextKey="true" BehaviorID="AutoCompleteBehaviorIDPlaceholder2" ID="AutoCompleteEx" TargetControlID="Filter_Customer" ServicePath="~/Webservices/Webservice_Selligent_Customers.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="3" CompletionInterval="1000" FirstRowSelected="true" EnableCaching="false" CompletionSetCount="20" DelimiterCharacters=";,:" CompletionListCssClass="autocomplete_completionListElement" CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem" CompletionListItemCssClass="autocomplete_listItem"> <Animations> <OnShow> <Sequence> <%-- Make the completion list transparent and then show it --%> <OpacityAction Opacity="0" /> <HideAction Visible="true" /> <%--Cache the original size of the completion list the first time the animation is played and then set it to zero --%> <ScriptAction Script="// Cache the size and setup the initial size var behavior = $find('AutoCompleteBehaviorIDPlaceholder2'); if (!behavior._height) { var target = behavior.get_completionList(); behavior._height = target.offsetHeight - 2; target.style.height = '0px'; }" /> <%-- Expand from 0px to the appropriate size while fading in --%> <Parallel Duration=".4"> <FadeIn /> <Length PropertyKey="height" StartValue="0" EndValueScript="$find('AutoCompleteBehaviorIDPlaceholder2')._height" /> </Parallel> </Sequence> </OnShow> <OnHide> <%-- Collapse down to 0px and fade out --%> <Parallel Duration=".4"> <FadeOut /> <Length PropertyKey="height" StartValueScript= "$find('AutoCompleteBehaviorIDPlaceholder2')._height" EndValue="0" /> </Parallel> </OnHide> </Animations> </ajaxToolkit:AutoCompleteExtender> </asp:Panel> <br /> <asp:Panel ID="Pnl_TabContainer" runat="server"> <ajaxToolkit:TabContainer ID="Sites_TabContainer" runat="server" OnActiveTabChanged="Sites_TabContainer_TabChanged" AutoPostBack="false"> <ajaxToolkit:TabPanel ID="TabPanel1" runat="server" HeaderText="DELETE"> <ContentTemplate> <asp:Label ID="Label2" runat="server" Text="DELETE" /> </ContentTemplate> </ajaxToolkit:TabPanel> </ajaxToolkit:TabContainer> </asp:Panel> <asp:Button ID="EditDescription_btnShowPopup" runat="server" Style="display: none" /> <ajaxToolkit:ModalPopupExtender ID="EditDescription_mdlPopup" runat="server" TargetControlID="EditDescription_btnShowPopup" PopupControlID="EditDescription_pnlPopup" CancelControlID="EditDescription_btnCancel" BackgroundCssClass="modalBackground" OkControlID="EditDescription_btnDummySave" /> <asp:Panel ID="EditDescription_pnlPopup" runat="server" Width="820px" Style="display: none"> <asp:UpdatePanel ID="EditDescription_Modal_UpdatePanel" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:Panel ID="Panel2" runat="server" Style="background-color: white; padding: 5px;"> <asp:HiddenField runat="server" ID="EditDescription_Modal_ArtID" /> <asp:HiddenField runat="server" ID="EditDescription_Modal_IsVersion" /> <asp:HiddenField runat="server" ID="EditDescription_Modal_ControlID" /> <asp:Table runat="server" ID="EditDescription_ModalTable" Width="100%"> <asp:TableRow CssClass="td"> <asp:TableCell ColumnSpan="2"> <asp:Label runat="server" ID="EditDescription_LblTitle" Text="" BackColor="#B5C7DE" Width="100%" Font-Bold="true" /> </asp:TableCell> </asp:TableRow> <asp:TableRow CssClass="td"> <asp:TableCell ColumnSpan="2"> <cc1:SmallHTMLEditor ID="EditDescription_Txt_Custom_Description" runat="server" ClientIDMode="AutoID" Height="500" Width="800" AutoFocus="False" /> </asp:TableCell> </asp:TableRow> </asp:Table> </asp:Panel> </ContentTemplate> </asp:UpdatePanel> <div style="text-align: left"> <asp:Button ID="EditDescription_btnSave" runat="server" Text="Save" Width="130px" CausesValidation="true" ValidationGroup="EditDescription_Create" OnClick="EditDescription_btnSave_Click" /> <asp:Button ID="EditDescription_btnDummySave" runat="server" Text="DummySave" Style="display: none" /> <asp:Button ID="EditDescription_btnCancel" runat="server" Text="Cancel" Width="70px" OnClick="EditDescription_btnCancel_Click" /> </div> </asp:Panel> </asp:Content>neilpeterson
Member
282 Points
51 Posts
Re: Can't find nested control on page
Mar 29, 2012 06:49 PM|LINK
use client id instead of putting whole dynamic name, i.e:
document.getElementById(‘<% controlname.ClientID%>’)
neilpeterson
Member
282 Points
51 Posts
Re: Can't find nested control on page
Mar 29, 2012 06:49 PM|LINK
use client id instead of putting whole dynamic name, i.e:
document.getElementById(‘<% controlname.ClientID%>’)
2pac
Participant
1586 Points
269 Posts
Re: Can't find nested control on page
Apr 03, 2012 01:40 AM|LINK
Hi Yannick,
Whenever you use MasterPage all the controls has a prefix like this
For example, if I want to find a control, which has a ID "customLabelControl" in my aspx page, in code behind
var masterPage = Page.Master; if (masterPage != null) { var findLabelControl = (CustomLabelControl) masterPage.FindControl(ctl00$MainContent$customLabelControl); //Cast to my custom controlfindLabelControl.Text = "I found my Custom Label Control";
}
Regards,
Jayesh