0

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

Tom Cruise
  • 1,395
  • 11
  • 30
  • 58

1 Answers1

0

You must also give id before adding control to the master page in order to find the control later

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

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.

Sid M
  • 4,354
  • 4
  • 30
  • 50
  • result is not a single server control but it contains a list of server controls.How can i specify client id for all these? – Tom Cruise Nov 04 '13 at 09:02
  • are all controls you are adding textbox and is there any fixed number of controls you are adding to your master page? – Sid M Nov 04 '13 at 11:00
  • made changes in the answer,try it. – Sid M Nov 04 '13 at 11:25
  • You can not provide ClientNames in such way. That's dangerous, as ASP.NET is better knows how would it operate with them. (If you really need to get any values from client to server part). But if you don't need that, you can create your own control with inheritance from TextBox and override "ClientID" property giving it your own name. But that has no much sense. If you just need to have that ClientID (just register some javascript to notify the control real client name). – Agat Nov 04 '13 at 11:35