views:

1244

answers:

1

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>
                    &nbsp;
                    <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;
}
+1  A: 

Unless you're calling goToSelectedPage in your page's Init handler, or else it's part of a page setup routine that is executed the exact same way on every page load, then your dynamically created control ct does not exist on the postback.

Remember that every time you post, you are getting a new instance of a Page, with brand new instances of all the controls on it. If you're not recreating and adding your usercontrol the same way every time, it simply won't be there.

womp
Hmm. Thanks! This is exactly what I needed to know. I'm new to ASP and the page life cycle still gets me in trouble from time to time.
Jisaak
Don't worry - the page lifecycle will get in you trouble for months and months to come ;)
womp