[RESOLVED] Access to master page's controls

I have a MasterPage that has a h2 tag. How can I access to this h2 form its Content Page?


Thank u

Hi,


You can't directly access the H2 tag, as it is a html control, it would need to be marked as runat=server.


Secondly, you wouldn't be able to access it from the content page as it is in the master pages control collection, you would need to iterate over the controls, or use findcontrol on its container to get to it.


A better solution would be to add a literal control either inside the h2 tag, or in place of the h2 tag and have the master page output the h2 when the page is loaded.  You could then add a public property to the masterpage to allow the Page to set either a string member that you will assign to the literal control or access the literal control directly.


Note, you will need to either cast the masterpage back to its type or add the masterpage directive to the page.


Happy to provide samples if you need it.


Regards


Si

Hi,

Give and Id and Put RunatServer tag and then use "Findcontrol"

http://msdn.microsoft.com/en-us/library/486wc64h.aspx

and Added to HtmlGeneric Control..

http://msdn.microsoft.com/en-us/library/7512d0d0%28v=VS.100%29.aspx

make your H2 a runat="server" by using

<asp:Literal id="h2Control" text ="<h2>Hello World</h2>"

control, and then, try Master.FindControl("h2Control") to find it from the content page

and this is the ContentPage's code behind:

}




so, 

if 2 , then see what type it is

Hi,


Try this.


In your master page .master file



<h2>
<asp:literal runat="server" id="uxMyPageTitle" mode="Transform" />
</h2>

In your Master page code file (not this is c#, you will need to convert it if you use VB


public string PageTitle
{
   get {
          return uxMyPageTitle.Text;
   }
   set {
         uxMyPageTitle.Text = value;
   }
}


So in your content page, you can do the following:

 protected void Page_Load(object sender, EventArgs e)
    {
          ((YourMasterPageClassHere)Master).PageTitle = "My Page Title";
}


Regards


Si

I chenged the code as bellow:

HtmlGenericControl h2 = (HtmlGenericControl)Master.FindControl("MyPageTitle");
h2.InnerText = "Forums";

But h2 is null.

if your "H2" control is inside other controls then you are likely to get this error, so, first give SlickSim's approach a shot , or do a FindControl("ParentControl").FindControl("child") in debug untill u reach the H2.

thoughts on "[RESOLVED] Access to master page's controls"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>