Using C# FindControl to find a user-control in the master page
- by Jisaak
So all I want to do is simply find a user control I load based on a drop down selection. I have the user control added but now I'm trying to find the control so I can access a couple properties off of it and I can't find the control for the life of me. I'm actually doing all of this in the master page and there is no code in the default.aspx page itself. Any help would be appreciated.
MasterPage.aspx
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager runat="server">
</asp:ScriptManager>
</div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false"
OnLoad="UpdatePanel2_Load">
<ContentTemplate>
<div class="toolbar">
<div class="section">
<asp:DropDownList ID="ddlDesiredPage" runat="server" AutoPostBack="True" EnableViewState="True"
OnSelectedIndexChanged="goToSelectedPage">
</asp:DropDownList>
<asp:DropDownList ID="ddlDesiredPageSP" runat="server" AutoPostBack="True" EnableViewState="True" OnSelectedIndexChanged="goToSelectedPage">
</asp:DropDownList>
<br />
<span class="toolbarText">Select a Page to Edit</span>
</div>
<div class="options">
<div class="toolbarButton">
<asp:LinkButton ID="lnkSave" CssClass="modal" runat="server" OnClick="lnkSave_Click"><span class="icon" id="saveIcon" title="Save"></span>Save</asp:LinkButton>
</div>
</div>
</div>
</ContentTemplate>
<Triggers>
</Triggers>
</asp:UpdatePanel>
<div id="contentContainer">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load" UpdateMode="Conditional"
ChildrenAsTriggers="False">
<ContentTemplate>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lnkHome" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="rdoTemplate" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
MasterPage.cs
protected void goToSelectedPage(object sender, System.EventArgs e)
{
temp1 ct = this.Page.Master.LoadControl("temp1.ascx") as temp1;
ct.ID = "TestMe";
this.UpdatePanel1.ContentTemplateContainer.Controls.Add(ct);
}
//This is where I CANNOT SEEM TO FIND THE CONTROL ////////////////////////////////////////
protected void lnkSave_Click(object sender, System.EventArgs e)
{
UpdatePanel teest = this.FindControl("UpdatePanel1") as UpdatePanel;
Control test2 = teest.ContentTemplateContainer.FindControl("ctl09") as Control;
temp1 test3 = test2.FindControl("TestMe") as temp1;
string maybe = test3.Col1TopTitle;
}
Here I don't understand what it's telling me. for "par" I get "ctl09" and I have no idea how I am supposed to find this control.
temp1.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
string ppp = this.ID;
string par = this.Parent.ID;
}