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;
Related
I am trying something which is starting to get weird but in principle it sounds normal. Basically I have a MasterPage in my ASP.NET application that takes care of showing the usual Login/Logout box in all pages. When the user is logged in, the usual "Welcome {Name}" appears.
The logged-in user details come from the Session.
Now I have a profile page where the user can change his/her name, which is all normal. As part of the Page Postback after editing, I update the Session with the new User Details.
What I would like to see happening is that the "Welcome" message would show the new Name if the user changed it.
I have digged a bit in the lifecycle and indeed given that the Session is accessed in the Page_Load but then updated in the UpdateButton_Click, the Welcome message is already updated before the Session is changed.
Does anyone have any idea on how to force a refresh for the Master Page or maybe there is something else I need to consider in terms of design?
I have also tried putting the Login/Logout box into a UserControl but things did not change.
Here are more details as requested:
MasterPage on PageLoad (accountMenuTitle is just a Label):
var loggedInUser = (Customer) Session["LoggedInUser"];
accountMenuTitle.InnerHtml = loggedInUser.Name;
ProfilePage Button_Click:
var updatedCustomer = update_Customer_Profile(txtFirstName.Text,
txtLastName.Text, txtAlternateEmail.Text, ... etc. etc.);
Session["LoggedInUser"] = updatedCustomer;
So when I click the button in the profile page, the page reloads, the logged-in User is updated in the Session but the Master Page Load already happened and the label shows the old name.
Dont set a Control in Page_load but directly render the value in the MasterPage.
MasterPage
<div class="title">
<h1>
<% if (Session["UserName"] != null)
{ %>
<%= Session["UserName"] %>
<% } %>
</h1>
</div>
Eventhandler
protected void Button1_Click(object sender, EventArgs e)
{
Session["UserName"] = this.TextBox1.Text;
}
The solution I implemented in the end relies on the Master Page exposing a method for updating itself.
Master Page:
public void UpdateLoginPanel()
{
if (Session["LoggedInUser"] == null) // logged out
{
accountMenuTitle.InnerHtml = "Log in";
}
else // logged in
{
var loggedInUser = (Customer) Session["LoggedInUser"];
accountMenuTitle.InnerHtml = loggedInUser.Name;
}
}
In the Profile page aspx file, you can expose your Master Page like this:
<%# MasterType VirtualPath="~/CustomerPortal.Master" %>
Then in the codebehind button click:
var updatedCustomer = update_Customer_Profile(txtFirstName.Text,
txtLastName.Text, txtAlternateEmail.Text, ... etc. etc.);
Session["LoggedInUser"] = updatedCustomer;
Master.UpdateLoginPanel();
I'm still a bit undecided if this is cleaner than Malcolm's answer to be honest :/
Hi way you have chosen here is correct..
You have defined Master directive so all properties and other access you have now..
One thing I want to share with you why you not set master page property directly from content page instead of calling UPDATEPANEL method(anyway choice is your)
Other thing I can see you have used session...you cam use VIEWSTATE.
How to call master page method in content page but the content page is not inheritted from the master page. what i have to do to call the master page method in content page
this is my Operation master page method
public void RedirectPage(string url)
{
ifrmMstr.Attributes.Add("src", url);
}
And this is my content page and i am calling the function like this in the page load
Operation master = new Operation();
master.rpage("../Default.aspx");
Does this help?
https://www.google.co.nz/#q=expose+control+from+master+page+to+content+page
"....To access the Master Page's methods or properties from a content page, reference the Master Page through the Page.Master property. This property returns an object of type MasterPage, so you'll need to explicitly cast it to the appropriate type before calling its methods or referencing its properties. Alternatively, you can set the #MasterType directive, which adds a property to the auto-generated ASP.NET code-behind class code named Master that is a strongly-typed reference to the specified Master Page...."
This will also be useful
How to control elements on a asp.net master page from child page
You can change the Master page frame's source from content page by calling a master page's JavaScript function and doing it client side.
Add something like this in your content page method (vb.net):
Dim frameSourceMethod As String = String.Format("YourMethod('{0}');",frameSourcePath)
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "displdcccdsse4errnfokf", frameSourceMethod , True)
I have my master page. I need to assign the logged user from the content page.
I am using the following code from the landing page
var currentUser = Membership.GetUser();
Label lblLoggedUser = (Label)Master.FindControl("lblLoggedUser");
lblLoggedUser.Text = currentUser.UserName;
But It goes off while I redirect to another page.
How can I set the master page content from one single page ?
PS- I am using membership providers for teh log in.
I believe what you are saying is that when the user visits the landing page then your listed code executes and sets the user name into the master page. But when they leave the landing page the user name becomes blank. You are trying to set the value once into the master page from the landing page and have it remain for subsequent pagas.
A better approach it to just have the master page set that value. Having the master page be responsible for master page controls frees the individual pages to focus on just their logic.
Move the code into the master page's Page Load or PreRender event.
void Page_PreRender(object sender, EventArgs e)
{
var currentUser = System.Web.Security.Membership.GetUser();
if (currentUser != null && currentUser.IsOnline)
this.lblLoggedUser.Text = currentUser.UserName;
else
this.lblLoggedUser.Text = "not authenticated";
}
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.
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" %>