updating master page image from content page - asp.net

Have made several attempts to update an image in a master page div when switching between content pages. I first tried creating a master page property that I could update from the content page's Page Load event by declaring "MasterType VirtualPath=", but since the master is already loaded by this time it wasn't going to work. It did work when I set the ImageUrl in the master page's page load event (if !Page.IsPostBack then set the image's url attrib), so I know it can work, but I need the image to change for every content page I visit.
Then I tried using the master's menu button click events to set the ImageUrl before loading the content page but this also had no effect. I saw a thread suggesting the use of an UpdatePanel to hold the image, so I may try that next. What's the best way to do this..??
I wouldn't be surprised if a better way is to have the image in a content div instead, and updating that from the master. Any suggestions or links would be most welcome. I can post code if anyone would like to have a look. thanks.

I don't know why you found it difficult. There are many ways to do this but I'll only show one. I just tested this and it worked. How?
In your master page, define your image and add runat="server"
<img src="put your default image.jpg" runat="server" id="changingImage" />
In your content pages, do this
protected void Page_Load(object sender, EventArgs e)
{
HtmlImage img = Master.FindControl("changingImage") as HtmlImage;
img.Src = "~/images/imageForContentPage1.jpg"; //replace this image based on your criteria
}
Possible exception is Null Reference when the name of the image control specified in .FindControl could not be found. Make sure it's exactly as you named it in the Master Page. And to prevent a Null Reference Exception, wrap a check around
if(img != null)
{
img.Src = "~/images/imageForContentPage1.jpg";
}

Related

Aspx file in two master pages

I have a page on a website that uses a master page and a child page. I would like to use the child page to create a new page on the website, but with a different master page.
Is it possible to do this without duplicating the code for the child page?
To make it clearer I have also added a link to a picture of the layouts used for the two pages.
In the picture, both children use the same code.
Edit: I have also thought about using a control for this, but I am not sure if this is the proper solution. The child page is pretty big and complex and also uses a lot of JavaScript.
When loading the child page, you can set the master page dynamically in code, in the PreInit event. Something like this:
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/NewMaster.master";
}
That way your child page can set its master page based on whatever condition you would have in your site.

Checking Request.ServerVariables["HTTP_REFERER"] on every request

I am using this code to check if the request came from a page , if not then redirect somewhere.
string referer = Request.ServerVariables["HTTP_REFERER"];
if (string.IsNullOrEmpty(referer))
{
Response.Redirect("/UnauthorizedAccess.aspx");
}
It is working , I don't know whether it is perfect the solution.However I am checking this on load event of one of my page.How can I make it check on every request.Should I check this for all my pages.Also it is a good approach.Can anybody point me in the right direction.Any suggestion is welcome.
If you have logic that you would like to be run on the OnLoad of a bunch of your pages. You should probably create a BasePage that derives from Page and have the logic inside. Then all the pages you want that logic in can derive from BasePage instead of the regular Page.
Another approach can be using Master Pages
Note: After reading OPs additional comments. One thing to look out for when using a Master Page is that the Master Page's Page_Load event happens AFTER the Content Page's Page_Load event.
In other words the lifecycle is like this:
Master Page Init Event
Content Page Init Event
Content Page Load Event
Master Page Load Event
If your response.redirect moves the user to another page with the same master page (and same "validation" check) you might find yourself in an endless loop :)
If you have lot of pages, with these kind of common codes, than one possible solution is creating your own MyPage class as a child of the standard Page class. In your MyPage you can use something like:
Page_Load(object sender, EventArgs e)
{
string referer = Request.ServerVariables["HTTP_REFERER"];
if (string.IsNullOrEmpty(referer))
{
Response.Redirect("/UnauthorizedAccess.aspx");
}
base.Page_Load(sender, e);
}
Then any of your pages can inherit from this own MyPage class instead of the .NET's standard one.
In this way the common code reside in one place. In case of any change you have to modify that only there.
Or another possibility, you can consider using Master Pages.

Access body element from content page via a nested master page

All I want to do is access the <body> element from the code-behind of a content page and add a class name to it.
I have a top-level master page with the <body> element in it. Then I have a nested master page which is the master page for the content page. From the code behind of the content page I want to add a class name to the body element. That's all.
I have this in the top-level master:
<body id="bodyNode" runat="server">
I added this to the code-behind for the content page:
Master.bodyNode.Attributes.add("class", "home-page");
And I get a message that:
System.Web.UI.MasterPage' does not contain a definition for 'bodyNode
If I add this to the aspx content page:
<% # MasterType VirtualPath="~/MasterPage.master"%>
The message then changes to:
bodyNode is inaccessible due to its protection level
Please advise, I've wasted like 2 hours on what feels like something that should be really simple to do :(
once you have set runat="server" for your body node, you have to access it using the HTMLControls namespace. try this.
public void Page_Load(Object sender, EventArgs e)
{
//Inject onload and unload
HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("bodyNode");
body.Attributes.Add("class", "home-page");
}
EDIT
Your problem is that you have nested master pages.
Since the "body" tag is in your top level master page, Master.FindControl() won't work, as that is looking in the nested master page.
What you need to do is use Master.Master.FindControl(), or recursively loop through your master pages, going up until Master.Master is null (as then you know you are at the top level master page) and then calling FindControl() on that.
I would add a public property to the code behind of the master page that would allow access to the body tag that is part of the master page. And then call that property from the content page.

How to change the Master Page dynamically

I want to assign one master page dynamically for a pure aspx file, Anybody can tell me, how to do this?
You can override OnPreInit in your default.aspx.cs and set the master page based on some value in your querystring. Something like this:
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
if (Request.QueryString["Master"] == "Simple")
MasterPageFile = "~/Masterpages/Simple.Master";
}
EDIT: the cause of your error message might be covered by this question.
I Left the ContentPlaceholder to add on it.. Actually , I tried to assign master page without using ContentPlaceHolder.. Now, I realised that, atleast one ContentPlaceholder should be there temporarily, even though we will change the master page dynamically...

Whats the best way to update an ASPX page when its child ASCX controls need to trigger a change?

When I have a child .ASCX control that needs to affect something in the parent page I'm not completely sure how I am supposed to handle the event flow in the case where I need to update something in the parent page.
What I've always ended up doing is putting logic into the 'Pre_Render' event handler.
Since this is processed after any child .ascx controls are processed I can be sure to render the ASPX page correctly before it displays. I just dont think this is a good design and I've always cringed when I've had to do it. But now there is stackoverflow so i can finally ask it!
For instance lets say I have a 'login control' ascx control in a page. The containing page displays a text label in the header bar for 'current logged in user'.
Lets say I click the 'login' button, which will then trigger my authentication and log my user in. The problem is that the text label in the parent page has already been rendered as 'No user logged in'. Thats no good!
By putting the logic into 'PreRender' it will be rendered after the user has logged in. I just dont like this because thats not what PreRender is for.
What is the intended best practice here that I'm missing? I know I could put an event handler on the user control, but that seems clumsy too because there'd be too much coupling.
PS. I'm just using this as an example. I'd had this problem numerous other times so please dont reply telling me how to implement login !
In your ascx.cs:
public delegate void NavigateEventHandler(int PID); // if you want a custom handler
public event NavigateEventHandler onNavigate;
In your page.aspx.cs:
protected void Page_Init(object sender, EventArgs e) {
eSelector1.onNavigate += new PostSelector.NavigateEventHandler(eSelector1_Navigate); }
public void eSelector1_Navigate(int PID) {
eSelector1.PopulateComments(eSelector1.m_PID); }

Resources