【问题标题】:Using C# FindControl to find a user-control in the master page使用 C# FindControl 在母版页中查找用户控件
【发布时间】:2010-03-31 05:26:27
【问题描述】:

所以我想做的只是找到一个基于下拉选择加载的用户控件。我添加了用户控件,但现在我正在尝试找到该控件,以便我可以访问它的几个属性,但我一生都找不到该控件。我实际上是在母版页中完成所有这些操作,而 default.aspx 页面本身没有代码。任何帮助将不胜感激。

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;
        }

这里我不明白它在告诉我什么。对于“par”,我得到“ctl09”,我不知道我应该如何找到这个控件。 temp1.ascx.cs

 protected void Page_Load(object sender, EventArgs e)
        {
string ppp = this.ID;
                string par = this.Parent.ID;
}

【问题讨论】:

    标签: asp.net user-controls updatepanel master-pages findcontrol


    【解决方案1】:

    除非您在页面的Init 处理程序中调用goToSelectedPage,否则它是页面设置例程的一部分,在每次页面加载时都以完全相同的方式执行,那么您动态创建的控件ct 不会存在于回发中。

    请记住,每次发布时,您都会获得一个 Page 的新实例,其中包含所有控件的全新实例。如果您不是每次都以相同的方式重新创建和添加用户控件,那么它根本就不存在。

    【讨论】:

    • 嗯。谢谢!这正是我需要知道的。我是 ASP 新手,页面生命周期仍然不时让我遇到麻烦。
    • 别担心 - 页面生命周期会给您带来数月和数月的麻烦;)
    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    相关资源
    最近更新 更多