Change label in masterpage in just one content page - c#

How do I define a label in a masterpage from inside one control page, withouth losing it when I navigate to another control page? I know that I can use this code and it works:
(Master.FindControl("myControl") as Label).Text = "someNewContent";
But I have to define this on every page to keep the same content in the label. Is there a easier/shorter way to define this piece of code only one time in the whole program? Thanks in advance.

I think I get the gist of what you're asking:
Firstly, I would strongly type the master page, in your content page just below the #Page directive, by using the #MasterType directive:
<%# MasterType TypeName="*fully qualified type of your master page*" %>
Next, place a public property on your master page, like so:
public string MyText
{
set { this.ViewState["TheText"]; }
}
In your content page (during the Page_Init, for instance) you can add:
this.Master.MyText = "Whatever you want to say!";
Then load your master pages control text property in the Page_Load event:
protected void Page_Load(object sender, EventArgs e)
{
this.myControl.Text = Convert.ToString(this.ViewState["TheText"]);
}
This won't persist from content page to content page because each content page instantiates a new instance of the master page. In that case, put whatever text you want to persist in Session, then read it in the mater pages Page_Load event.
Hope this answers your question.

Related

Is it possible to remove a user control (.ascx) present in master page (.master) from content (.aspx) page?

I have a master page which is being inherited to a lot of content page. I have one particular page in which I do NOT want one of the user controls present in the master page. I do NOT want to create a separate master page for this. I want to know if there's any way to prevent the user control present in master page being inherited to child / content page. Appreciate your help. thanks !
In the user control,
protected void Page_Init(object sender, EventArgs e)
{
Css.Register(this, "/css/reset.css", BrowserTarget.All, true);
}
In the master page, the user control is added
The part (css in reset.css) I'm trying to remove / prevent being inherited to
the content page
ol, ul {
list-style: none;
}
You can use in the child control:
((UserControl)this.Master.FindControl("userControlName")).Visible = false;
In order for the control to never exist at all with what you're saying and your feedback that hidden doesn't fit your needs, I'm guessing removing the control won't either.
The only other option here is to change the master page to have a flag that loads the control. This changes the approach from "remove this if" to "include this when." Then, in your calling pages, you'll need to set that flag. To make this new change less invasive, set the default the true and in this page set it to false.
Try this
MasterPage master = this.Master;
UserControl userControl = (UserControl)master.FindControl("userControlName");
if(userControl != null)
{
master.Controls.Remove(userControl );
}

Globalization between MasterPage and Content Page

I have a master page and a few content page with their own control. Inside the master page I have a dropdown list that contain the language the user can choose. So when the user clicks on it the website content language will change accordingly.
My problem is that I can successfully change my website content by using UIculture and culture in the master page when I select other language, but I have no idea how do i change the culture from the content page since the control (dropdownlist) that determine the culture is from the master page. When I debug it seems that the content page Page_Load will run first follow by the few coding in the master page to change the language inside there.
Follow these steps:
In the Master Page, create a public string property that returns the culture name (e.g ChosenCulture).
In the content page, add a MasterType directive <%# MasterType VirtualPath="~/YourMasterPage.master" %> Then you'll be able to access MasterPage properties via Master. inside the Content Page
In the content page overrides InitializeCulture method and set UICulture property.
protected override void InitializeCulture()
{
UICulture = Master.ChosenCulture;
Culture = Master.ChosenCulture;
base.InitializeCulture();
}
To maintain the value of ChosenCulture between posts you can use Session:
MasterPage:
public string ChosenCulture
{
get { return Session["ChosenCulture"]; }
set { Session["ChosenCulture"] = value; }
}
In the DropDownList's OnChanged event, just set ChosenCulture property:
ChosenCulture = MyDropDownList.Value;

When in the page lifecycle we can assign master page?

I know ViewState is available between InitComplete and Preload events in LoadViewSate method. Similarly, I want to know in which page lifecycle event can we assign a master page for a particular page?
Because the master page and content page are merged during the
initialization stage of page processing, a master page must be
assigned before then. Typically, you assign a master page dynamically
during the PreInit stage
On Page PreInit event
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/MyMaster.master";
}
Read Working with ASP.NET Master Pages Programmatically
From: ASP.NET Page Life Cycle Overview
Page Event
Typical Use
PreInit
Raised after the start stage is complete and before the initialization stage begins. Use this event for the following:
Check the IsPostBack property to determine whether this is the first time the page is being processed. The IsCallback and IsCrossPagePostBack properties have also been set at this time.
Create or re-create dynamic controls.
Set a master page dynamically.
Set the Theme property dynamically.
Read or set profile property
values.
Note If the request is a postback, the values of the controls have not yet been restored from view state. If you set a control property at this stage, its value might be overwritten in the next event.
From: Attaching Master Pages Dynamically
In addition to specifying a master page declaratively (in the # Page directive or in the configuration file), you can attach a master page dynamically to a content page. Because the master page and content page are merged during the initialization stage of page processing, a master page must be assigned before then. Typically, you assign a master page dynamically during the PreInit stage, as in the following example:
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/DefaultMaster.master";
}
Edit:
Source: ASP.NET Master Pages - How Master Pages Work
You can use #Page directive also to specify master page.
<% # Page Language="C#" MasterPageFile="~/Master.master" Title="Content Page 1" %>

how to update content page from master page?

Menu links are in the master page and and the corresponding click events are written in the master page itself. I have only one content page. Data is saved in html form in the DB and that will be rendered to the content page on corresponding menu link clicks. But the problem is content page load occurs before the master page so the content page is blank. Here my code-
public void GetLinkPage(Int32 linkId)//master page
{
LinkContentEnitity linkContent = new LinkContentEnitity();
linkContent = PageController.GetPageContent(linkId);
}
linkContent contains that content page in HTML form. How can I print this value on content page?
Though it's not entirely clear, it sounds like you are saying your content page needs information in its own Load event that is generated in the Page_Load event of the master page?
So either move the code that calls GetPageLink() to PreRender in your content page, or add an event handler in ContentPage for Page_LoadComplete() (which fires after all child controls on a page are loaded) and call GetPageLink() and do your rendering from there, e.g. in content page:
protected override void OnInit(EventArgs e)
{
Page.LoadComplete += new EventHandler(Page_LoadComplete);
}
protected void Page_LoadComplete(object sender, EventArgs e) {
// do stuff here, instead of OnLoad/Page_Load event
}
By the way this is a useful reference for event order. The problem you are having is very easy to get into when dealing with nested controls (master/content, usercontrols, etc), it helps to have a good understanding of the event order.
http://msdn.microsoft.com/en-us/library/dct97kc3.aspx
One way to do it might be this... On your content page, add a public method that does the work you need done. In the master page, cast this.Page to your page class and call that method.

Can I change the text of a label in a masterpage when loading a content page?

I have a label in a master page (sample.master) called lblHeading.
I want to dynamically change the text of the label when I load the content page.
I need to do this because I want to change the heading to something meaningful but only after I know about the content of the page.
Is this possible?
yes, you can in this very simple way........
((Label)Master.FindControl("lblHeading")).Text = "your new text";
Yes.
You want to create a strongly-type master page and you can then access it's properties from your content page during Page_Load or wherever else.
Yes, it is possible. MasterPage behaves just like UserControl in your page.
Possible steps to implement this:
Create a property or method on the MasterPage that enables you to make changes to the Label. E.g.:
public void ChangeLabel(string label) {
lblHeading.Text = label;
}
From your Page, get the reference to the MasterPage by using the Page.Master property.
Call the method defined in step 1 to change the MasterPage contents.
Additional info: You may need to cast Page.Master into your MasterPage type, try Coding the Wheel's link for instructions on how to do that.
Do as stated above. Then, e.g., from your page do this (master page has label with ID="Label_welcome"):
Label mpLabel = (Label)Page.Master.FindControl("Label_welcome");
if (mpLabel != null)
{
mpLabel.Text = "Welcome Fazio!";
}
You can create a public property in the masterpage that will change the label.
public string Heading
{
set
{
lblHeading.text = value;
}
}
It also depends on how deep your controls inside the Master page are. In my case, I had a Label control inside a ContentPlaceHolder control... so I had to do this...
Label myLBL = this.Master.FindControl("HeaderContent").FindControl("myLabel") as Label;

Categories