【问题标题】:Access Parent Page Control in Child User Control在子用户控件中访问父页面控件
【发布时间】:2013-06-26 05:25:52
【问题描述】:

我有一个aspx 页面,其中有一个Ajax UpdatePanel,它有一个AJAX Tabcontainer,它有5 个选项卡。在第一个选项卡中,我有一个 ProductID 的下拉列表。在第二个选项卡中,我使用了一个 UserControl,其参数需要根据 productID 进行反映。我想访问我没有得到的用户控件中的 DropDownList ProductID。我的部分代码是

DropDownList IDList = (DropDownList)(this.Parent.FindControl("ProductID");

这不起作用,我得到 NULL。我也试过了

DropDownList IDList = (DropDownList)(this.Page.FindControl("ProductID");

请告诉我该怎么做。

如要求的部分必要代码是

<asp:UpdatePanel ID="UpdatePanelTankFormula" runat="server">
    <ContentTemplate>
        <asp:tabcontainer id="TabContainerTankFormula" AutoPostBack="true" runat="server" OnClientActiveTabChanged="clientActiveTabChanged"   activetabindex="0" style="width:100%;">  

           <asp:TabPanel ID="InputDataTab" runat="server"  HeaderText="Data Input">
               <ContentTemplate> 
                   <asp:DropDownList id="TankNameCombo" DataTextField="TankName" runat="server" AutoPostBack="true" DataValueField="customertankid"   width="200px" onclick="javascript:SetHiddenField();">    </asp:DropDownList>
               </ContentTemplate> 
           </asp:TabPanel>

           <asp:TabPanel ID="LacticAcidAdditionTab" runat="server"  HeaderText="Lactic Acid Addition">
               <ContentTemplate> 
                   //My user control
                   <UC:TankNote runat="server" ID="LaticTankNotesUC" GetNoteType="LAC" MEQMode="False"></UC:TankNote> 
               </ContentTemplate> 
           </asp:TabPanel>

        </asp:tabcontainer>
    <ContentTemplate>
</asp:UpdatePanel>

现在,在这个用户控件的代码后面,我想访问这个 DropDownList,但我没有得到。对于修复,我定义了一个返回此列表值的公共函数。但它是一个修复而不是解决方案。

【问题讨论】:

    标签: c# asp.net findcontrol


    【解决方案1】:

    您将在选项卡控件中看到类似的内容。

     <cc1:TabContainer ID="TabContainer1" runat="server">
        <cc1:TabPanel ID="TabPanel1" runat="server">
            <ContentTemplate>
                <asp:DropDownList id="dropdownlist1" runat="Server"/>
            </ContentTemplate>
        </cc1:TabPanel>
    

    所以首先你需要找到 tabPanel1 然后找到 dropdownlist1 如下所示。

    TabContainer TabContainer1= (TabContainer)(this.Page.FindControl("TabContainer1");
    if (TabContainer1!=null){
    TabPanel TabPanel1= (TabPanel)(this.TabContainer1.FindControl("TabPanel1");
    if(TabPanel1 !=null){
    DropDownList dropdownlist1= (DropDownList)(this.TabPanel1.FindControl("dropdownlist1");
    
    }}
    

    【讨论】:

    • 我什至没有得到 TabContainer 控件,它显示为 NULL,我也尝试过更新面板,因为整个 TabContainer 都在 UpdatePanel 中,即使这样也不起作用
    • 是的,我做到了,甚至 UpdatePanel 控件也没有进入用户控件
    • 我已经用必要的代码更新了帖子。请检查。
    • 你应该得到更新面板。在哪种情况下您会尝试查找更新面板和子控件?
    • 是的,我应该得到它,但我不知道为什么它不来。我的用户控件中有一个按钮,即单击事件我需要 DropDownList
    猜你喜欢
    • 2011-06-29
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 2011-01-16
    • 2017-12-23
    • 1970-01-01
    相关资源
    最近更新 更多