Masterpage and Label control?

G

Guest

Hi,

I have Master page and a Label control on it.
I would like to set the Text property for that label from Default.aspx on
page load event where Default.aspx belongs to the Master page.

How can I reach the label control on the master page from other pages?

Thanks in advanced for any help,
Asaf
 
M

Mark Fitzpatrick

You can try to use the Page.Master property to access the generic master,
then cast it to the type of master such as ((myMaster)Page.Master).MyLabel

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
G

Guest

Hi Mark,

I am receiving the error:

'MasterPage.lblTest1' is inaccessible due to its protection level.

Regards,
Asaf
 
G

Guest

Hi Mark,

I have found the solution.

Label objLBL = (Label)Master.FindControl("lblTest");
objLBL.Text = "ABC";

Regards,
Asaf
 
S

Steven Cheng[MSFT]

Hi Asaf,

Thanks for sharing your solution with us.

As for data sharing between master page and concrete pages in ASP.NET 2.0,
you can also consider using custom properties. The "Page" class in ASP.NET
2.0 contains a "Master" property which refers to the page's master page(if
the page has applied a certain master page). Therefore, you can cast this
"Master" property into the exact type of the master page and access any
custom property defined in that master page class. e.g.

=======master page===========
public partial class App_Masters_MForm : System.Web.UI.MasterPage
{

public string TextBox1Text
{
get
{
return TextBox1.Text;
}
}
..................

========================

========concrete page=========
protected void Button1_Click(object sender, EventArgs e)
{
string txt = ((App_Masters_MForm)Master).TextBox1Text;

Response.Write("<br/>TextBox1Text: " + txt);
}
====================

The above master page class(App_Masters_MForm) uses a custom
property(TextBox1Text) to expose its inner control. Thus, the concrete page
can access that custom property through the converted master page property.

Hope this also helps.

Regards,

Steven Cheng
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.



Get Secure! www.microsoft.com/security
(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,780
Messages
2,569,611
Members
45,279
Latest member
LaRoseDermaBottle

Latest Threads

Top