简体   繁体   English

在子用户控件中访问父页面控件

[英]Access Parent Page Control in Child User Control

I have an aspx page in which I have Ajax UpdatePanel which has a AJAX Tabcontainer which has 5 tab. 我有一个aspx页面,其中有Ajax UpdatePanel,其中有一个具有5个标签的AJAX Tabcontainer。 In First tab I have a DropDownList for ProductID. 在第一个选项卡中,我有一个用于ProductID的DropDownList。 In the second tab I have used a UserControl whose parameter needs to be reflected based on productID. 在第二个选项卡中,我使用了一个UserControl,其参数需要根据productID反映出来。 I want to Access the DropDownList ProductID in the user control which I am not getting. 我想访问用户控件中的DropDownList ProductID,但我没有得到。 Part of my code is 我的代码的一部分是

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

this is not working and I am getting NULL. 这是行不通的,并且我得到NULL。 I also have Tried 我也试过了

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

Please tell me how I can do this. 请告诉我我该怎么做。

As asked Part of necessary code is 根据要求,必要代码的一部分是

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

Now in the Code Behind of this User Control I want to access this DropDownList, which I am not getting. 现在,在此用户控件后面的代码中,我想访问此DropDownList,但我没有得到。 For the fix I have define a Public function that return the value of this list. 对于此修复,我定义了一个Public函数,该函数返回此列表的值。 But its a fix not solution. 但这不是解决方案。

You will have something like this in tab control. 选项卡控件中将具有类似的内容。

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

So first you need to find tabPanel1 then find dropdownlist1 like following. 因此,首先您需要找到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");

}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM