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:
that dosnt work ,
try this
ContentPlaceHolder cph = this.Form.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
TextBox ctrlInput = (TextBox)cph.FindControl("mycontrol");
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.
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.
main()
ContentPlaceHolder content;
PlaceHolder pContent;
Pageload()
content = Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
pContent = content.FindControl("PHOptions") as PlaceHolder;
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;
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.
this will work,
TextBox TxT = (TextBox)FindControl(ControlName as it is defined in html code for your form);
ContentPlaceHolder cph = this.Form.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
TextBox ctrlInput = (TextBox)cph.FindControl("mycontrol");
The method above worked for me. Thanks!
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");
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");
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")
Thank you so much.. It worked for me.
this.Form.FindControl("labelid");
Post a Comment
<< Home