using master page - c#

I have master page in asp.net.i want to display user in master page but in my knowledge master page is loading once at a time.How it possible when Session["User"] only get when login attempt.But master page is already loaded.

The master page renders with each page request, so if you have code that dynamically populates data on the page (for example, setting a label in Page_Load) it will populate on each subsequent page load. A master page can read from Session values just fine.
Have you tried something specific and it's not working?

try to put a breakpoint in pageload of aspx.cs and master page. Page will load first. and after that master page will load. So session added from page can be accessible form masterpage

Related

How to set Master Page for a group of pages in asp.net?

I have 10 pages in my application and 2 master pages. I need to set master page dynamically to the pages. I can set using #page directive or this.MasterPage = "url" . But both these needs to be written in each page.
Any method to set the master page to the first 5 pages as MasterPage1 and other 5 pages as MasterPage2? This is easy of i have huge numbers of pages
i got. In web.config we have the option to set master page in Page tag of system.web section.

Refresh master page from content page

I want to refresh my master page from a User control which is on content page in update panel.
Problem is that my user control is in update panel and I cant take it out of that due to some reasons .Now I want to directly refresh my master page on button click of user control which is on content page.Thanks in advance.
The following javascript will refresh the master page...
<script type="text/javascript">
function reloadPage()
{
window.location.reload()
}
</script>

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" %>

Can I know what page is in my master page's ContentPlaceHolder?

All the Google finds I ran into tell me how to use FindControl to access a control on the master from the content page itself.
However, what I'm trying to do is the opposite.
From the master page, I want to reference whichever child page is in the ContentPlaceHolder.
Why you ask.
I want the master page to know which tab should be active depending on the content Page currently in the placeholder.
This lets me avoid having each page to reference the master page and allow them to change the active tab; that should be the master page's job (if there's a way it can know whom it's enclosing).
Thanks. No rants please.
If you are looking to get the instance of the executing page class, you can retrieve it from the current HTTP context:
var page = HttpContext.Current.CurrentHandler as Page;
From there, you can navigate the page's control tree, call FindControl(), and so on. Be cautious about page lifecycle, though, as master page events tend to fire before their page event counterparts.

Masterpage + updatepanel on child page

So I have a masterpage with a login that is in an update panel. I have a child page that has a literal control that should update when the login updates. What it doesn't do is reload the method I use to generate the content for that literal when it posts back. I tried to call the method on the child page from the master page once you click log in, but I get an error that the literal control cannot be found (because it exists on the child page not the master page). How would I reference that control in the masterpage to pass it to my method?
The article below shows how the control tree works with MasterPages and how to reference different controls at different levels of the control tree.
ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
So the scenario is that you have an update panel on the child page that when triggered doesn't update/refresh your lets say, label which is in your header on your master page.
What you do is, in the code behind of your master page create a function that changes the value of the label.
Include an update panel on the master page for the label, which is triggered by the textchanged event etc.
Now, in your child page code behind or your let say, button click event, call upon the function that exists in the master page and send the needed value in the parenthesis.
C#:
((MyMaster)this.Page.Master).ShowMessage(text);
VB.NET:
DirectCast(Me.Page.Master, MyMaster).ShowMessage(text)
This should update the label with the correct value whilst triggering the update panel on the master page and thus refreshing your label as well.
I'm about to try this now for myself, wish me luck. :D

Categories