setting the text value of a control on the master page, from a content page

A

Aussie Rules

Hi,

Within the code of a content page, how do you say set the .text value of a
label thats on the master page....

Thanks
 
G

Guest

Label MasterPageLabel = (Label)Master.FindControl("MasterPAgeLAbelID");
MasterPageLabel.Text ="some words";

Master.FindControl returns a reference to the master page control.
 
V

vMike

Aussie Rules said:
Hi,

Within the code of a content page, how do you say set the .text value of a
label thats on the master page....

Thanks
Another solution is to have a class for your master page (say its called
myMP) with
properties. Then in you content code you do the following.

In master page code

Public Property MyLabelText() as string
Get
return MyLabel.Text
End Get
Set (MyLabelText as string)
MyLabel.text = MyLabelText
End Set
End Property

In your content page code

dim mp as myMP = ctype(page.master, myMP)
mp.MyLabelText = "My label text"
 
G

Guest

vMike said:
Another solution is to have a class for your master page (say its called
myMP) with
properties. Then in you content code you do the following.

In master page code

Public Property MyLabelText() as string
Get
return MyLabel.Text
End Get
Set (MyLabelText as string)
MyLabel.text = MyLabelText
End Set
End Property

In your content page code

dim mp as myMP = ctype(page.master, myMP)
mp.MyLabelText = "My label text"

If I am designing common code-behind functionality, but like to vary the
look and feel of my master pages I find that having the master pages
implement a standard interface is a good way to go.

That way the properties are wrapped up in one place, and the content page
code need never change because they can always access the property regardless
of the master page they reside within. e.g.

((ISomeCommonMasterPageStuff)this.Master).Title = value;
 
W

Walter Wang [MSFT]

Thanks for other community members useful input.

By the way, you actually can instruct the ASP.NET page parser to generate a
strongly typed Master property in the content page by adding an @
MasterType directive:

<%@ MasterType VirtualPath="~/MasterPage.master" %>

Then you can directly use "Master.MyLabelText" to access the public
property of the master page without type cast.

However, since the Label control on the master page is protected, you still
need to wrap its Text property in a public property, like MyLabelText in
vMike's sample code.

I also think GTB's approach is better if you have several master pages.

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

And here's some useful links about ASP.NET master page:

#ASP.NET 2.0 - Master Pages: Tips, Tricks, and Traps
http://www.odetocode.com/Articles/450.aspx
http://www.odetocode.com/Articles/419.aspx


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top