Tuesday, May 30, 2006

Master Page & Find Control

this.FindControl("myControl");

I realy miss this sentence. Since I start using ASP.net 2.0 I’m not able to use it if try to get a control inside page that has master page, because I’ll get an error that says “Object reference not set to an instance of an object.” (did I see this error before? ;) , sure I did). This error occures because the control cannot be found!! Let me explain why:

When you apply a master page, that master page has a ContentPlaceHolder control, and your page has only an <asp:Content/> control. This Content control get stripped and discarded and never makes it to the final Page control structure. The contents of the Content control are merged into the ContentPlaceHolder. The result is that the page ends up with only 1 control in its Controls collection: a ContentPlaceHolder from the master page. (Thanks to Jeffrey Palermo for his article)

OK I get it. Now how can I reslove this issue???
The answer is by trying to search for your control inside the Form of the page. Check this out :


this.Form.FindControl("myControl");

Good Luck & Happy Programming.

12 Comments:

At 2:49 AM, Blogger oneforall2012 said...

that dosnt work ,
try this

ContentPlaceHolder cph = this.Form.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;

TextBox ctrlInput = (TextBox)cph.FindControl("mycontrol");

 
At 8:26 AM, Anonymous Anonymous said...

I'm having a similar problem in that my user control is unable to reference the page it's on. If the parent page is a master page, its parent will be a contentplaceholder instead of a page. Consequently, I am finding it difficult, if not impossible to create a single control that operates on both masterpages and standard forms.

 
At 11:43 PM, Anonymous Anonymous said...

Page.Master.FindControl("ctrlname")
This will work as long as the control is on the master and not nested in another control on the master.

 
At 1:03 AM, Blogger Raju.M said...

main()
ContentPlaceHolder content;
PlaceHolder pContent;

Pageload()
content = Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
pContent = content.FindControl("PHOptions") as PlaceHolder;

 
At 1:06 AM, Blogger Raju.M said...

main class()
ContentPlaceHolder content;
PlaceHolder pContent;

Pageload()

content = Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
pContent = content.FindControl("PHOptions") as PlaceHolder;

string findContl="myControl";
TextBox txtBox=pContent.FindControl(findContl)as TextBox;

 
At 4:30 PM, Blogger gigino said...

That's my solution:

ContentPlaceHolder MYcontent = (ContentPlaceHolder)this.Page.Master.FindControl("Content_YourContent");

TextBox YourTxt = (TextBox)MYcontent.FindControl("MyTxt");

string mystring = YourTxt.Text;

It worked for me.

 
At 3:51 PM, Anonymous Anonymous said...

this will work,
TextBox TxT = (TextBox)FindControl(ControlName as it is defined in html code for your form);

 
At 11:31 PM, Anonymous Anonymous said...

ContentPlaceHolder cph = this.Form.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;

TextBox ctrlInput = (TextBox)cph.FindControl("mycontrol");


The method above worked for me. Thanks!

 
At 9:40 AM, Anonymous Anonymous said...

Another way is to publish the place holder of the master page e.g.
public ContentPlaceHolder BodyHolder { get {return ContentPlaceHolder1;} }

than in the page with the master page use this code to find the controls
Button btn = (Button) ((MasterPage)this.Master).BodyHolder.FindControl("Button1");

 
At 9:42 AM, Anonymous Anonymous said...

Another way is to publish the place holder of the master page e.g.
public ContentPlaceHolder BodyHolder { get {return ContentPlaceHolder1;} }

than in the page with the master page use this code to find the controls
Button btn = (Button) ((MasterPage)this.Master).BodyHolder.FindControl("Button1");

 
At 2:24 AM, Anonymous Anonymous said...

I had the control I was after inside a ContentPlaceHolder, so I had to search for that first and then get the control inside of it:

this.Page.Master.FindControl("ContentPlaceHolderID").FindControl("ControlID")

 
At 12:29 PM, Anonymous Praveen James said...

Thank you so much.. It worked for me.
this.Form.FindControl("labelid");

 

Post a Comment

<< Home