简体   繁体   English

如何访问内容页面中母版页的动态加载的服务器控件?

[英]How to access Dynamically loaded server control of Master Page in content page?

I have created Master page and from content page, loaded some server controls to Master Page. 我已经创建了母版页,并从内容页创建了一些服务器控件到母版页。

 Control ctrl = Page.ParseControl(result);
 ContentPlaceHolder cph = (ContentPlaceHolder)this.Page.Master.FindControl("ContentPlaceHolder1");
            cph.Controls.Add(ctrl);

Now I need to access the Controls in Content page. 现在,我需要访问“内容中的控件”页面。 But the id specified is changed after parsed the controls. 但是,在解析控件后,指定的ID会更改。 It looks like below. 如下图所示。

 <input type="submit" name="ctl00$ContentPlaceHolder1$reset" value="reset" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ContentPlaceHolder1$reset&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="ctl00_ContentPlaceHolder1_reset" />

So how can i access these controls in content page? 那么如何在内容页面访问这些控件?

You must also give id before adding control to the master page in order to find the control later 在将控件添加到母版页之前,还必须提供ID,以便以后找到该控件

Control ctrl = Page.ParseControl(result);
ctrl.ID="ContentPlaceHolder1_reset";
ContentPlaceHolder cph = (ContentPlaceHolder)this.Page.Master.FindControl("ContentPlaceHolder1");
cph.Controls.Add(ctrl);

hope this works for you. 希望这对您有用。

Update: 更新:

If you are adding a list of TextBox, then you can provide id to TextBox as follows 如果要添加TextBox的列表,则可以按以下方式向TextBox提供ID

for (int i = 1; i < 11; i++)
    {
        TextBox t1 = new TextBox();
        t1.ID = "TextBox" + i;
        ContentPlaceHolder cph = (ContentPlaceHolder)this.Page.Master.FindControl("ContentPlaceHolder1");
        cph.Controls.Add(t1);
    }

Here I'm adding 10 TextBox to your master page. 在这里,我将10个TextBox添加到您的母版页。

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

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