This is either a weird behavior or I am doing something wrong here.
I have an aspx page with an associated Master page. I want to replace this master page with a new one.
The following steps I followed:
I created a new master page and added the same html from the old one.
I replaced MasterPageFile attribute in the page directive.
I thought this should work but it is not. It is still showing the old master page.
Now, when I replace the MasterPageFile from code it works.
public DefaultNew()
{
this.PreInit += new EventHandler(DefaultNew_PreInit);
}
void DefaultNew_PreInit(object sender, EventArgs e)
{
MasterPageFile = "~/_Master/MasterPageNew.Master";
}
I have rebuilt the code, closed Visual Studio and restarted but in Vain.
An ideas? Please help.
Check to see if you have a base class for the page that happens to sets the master page for all pages that derive from it.
Related
I have a hosting account with GoDaddy.com, IIS 7 server running .NET 4.0, and I am in the early stages of developing a web site for our church. The content is a free CSS based template I have moved into an ASP.NET Web App with Master pages. (If critique on content is necessary please keep in mind this is a very early stage of development...but I am open to any suggestions. :) )
For some reason, when I enter the full URL to the default page, the page renders properly. However, if I only enter the folder name without the page name, I only get the content form the page itself.
See for your self:
http://www.websmithsllc.com/lpacftp/Home.aspx
http://www.websmithsllc.com/lpacftp
I don't think this is an issue with my wire-up between the content page and the masterpage as it will properly render when I use the full URL. Therefore, I assume the issue is in one of three areas:
How I am publishing: One Click to an FTP directory
The project settings: Currently Home.aspx is the start page
An issue with the settings on my host.
I really hope the issue isn't #3 because my experience so far has been that their tech support is severely lacking in the area of Visual Studio / IIS development and publishing.
Now, some additional clues. I KNOW that the Site.Master file is being rendered, at least to some extent. The menu that is being displayed is created in the Site.Master.Page_Load event handler:
protected void Page_Load(object sender, EventArgs e)
{
//Load sidebar content
Page p = HttpContext.Current.CurrentHandler as Page;
menuContent.Text = Helpers.StaticHelperMethods.GenerateMenuContent(p.Title);
}
Static method:
public static string GenerateMenuContent(String pageTitle)
{
StringBuilder menu = new StringBuilder();
if (pageTitle == "Home")
{
menu.Append("Home\n");
}
else
{
menu.Append("Home\n");
}
More similar code...
In this case, p.Title should == "Home", but the code is responding as though is does not, and I don't know how (if I can) debug live to see what's going on. Finally, if you look closely at the second link, you'll notice some stock ASP.NET advertising text- that appears to be coming from the stock "Default.aspx" file in the BodyContent asp:Content object. However, looking at the properties/Web tab I can see that the startup action is Specific Page : Home.aspx.
So- hopefully I haven't added a ton of unnecessary info here, but at least enough for someone with more experience to help me figure out what I'm doing wrong here.
Thanks in advance for whatever help you can offer me on this.
You need a default page. Create it and then in the code behind in Page_Load write:
Response.Redirect("Home.aspx");
Or change your default page in IIS. Or change your home page to Default.aspx (and rename the class and the page directive).
Of the three, creating a Default.aspx page that redirects to Home.aspx is likely the easiest.
Yes, MatthewMartin is correct. Your hosting service's IIS is not configured to pick up "Home.aspx" as a default page. You will need to either get them to add it to the IIS configuration, or rename your home page to Default.aspx, or create a "dummy" Default.aspx that redirects to your Home.aspx.
I have website that has some issue to work with.
One of the issues is loading 5x.
I have been trying to set language speficic for the page.
My problem is as follows:
While page is refreshing n times it does somewhere overrides page language settings. I had as test modified global.asax page in Application_Start method. This method gets called only once.
After this page goes into default.aspx page and hits the page ntimes, when the page gets initialised it change culture to default culture en-GB instead to the one I have set in Page_Load event.
I have set the thread . current . UI and Culture to my specific culture. But this does't work.
I have tried:
Set language in global.asax
Set language in global.asax + onload page for my default.aspx
Use session to set language and after reload the page to display in correct page.
Any ideas?
If you add this to Global.asax.cs it should work. It works for me.
protected void Application_BeginRequest(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("de-ch");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("de-ch");
}
But your description of the issue is a bit confusing...
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.
I put a break point at the protected void Page_Load(object sender, EventArgs e) method of my master page, but when i start the site it does not hit that break point.
Why is the event not firing? I would like to use this event along with others such as the Init event in order to check to see if the session has expired everytime a page loads...
Thanks.
You can check that AutoEventWireup is set to true in the Master declaration.
<%# Master Language="C#" MasterPageFile="~/MasterPages/Main.master" AutoEventWireup="true" CodeFile="Main.master.cs" Inherits="MasterPages_Main" %>
If it is set to false you have to manually connect the events.
The problem is likely that your .aspx page has not correctly referencing your .master page. Be sure that, at the top of your .aspx page, you have a line similar to the following:
<%# Page Title="Some Title" Language="C#" MasterPageFile="Main.Master" CodeBehind="MyPage.aspx.cs" Inherits="MyApp.MyPage" %>
Another possible problem is that your .master page isn't referencing the proper (or any) assembly. Be sure that the top line of your .master page is similar to the following:
<%# Master Language="C#" AutoEventWireup="True" CodeBehind="Main.master.cs" Inherits="MyApp.Main" %>
A couple of things to check, some of which may be obvious...
Check your child page is calling the correct Master Page.
The Master Page Page_Load executes after the child Page_Load, so make sure you debug through the child page execution first.
Check that you've actually got your Page_Load event wired up if you're using VB.NET.
You might want to try creating a base class of type Page that handles your session check. Leave the master pages for page design. If you have multiple master pages, you would have to duplicate that code in each one, but if your pages inherit from a single base page, your session check logic will be in one place.
I had the same problem - the pageload was firing before but something went wrong and it stopped.
What fixed it was putting the page_load event in the .master file not the .master.cs
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//put your code here
//any function u wanna call declare it in the code file as public
}
</script>
I had a slightly different problem and different solution.
Just in case anyone has similar situation as me.
I had a nested master page and the control and related event method were in the "middle" master. The methods did NOT get called when they were placed in .cs file for middle master page. But they got called when included in .master page within script tags as described above by "petra".
This seems to be more of a bug in .net platform - Also - I do not think some of the above complicated solutions are (or should be) needed (e.g., keeping code out of master page and using master page only for structure etc.) - that is more of a workaround and I suspect there indeed is a bug in .net platform with respect to master page event firings (specially with nested master pages as in my case).
You need to check declaration of page to make sure it refers proper masterpage and masterpage, to make sure that it refers proper inherited class.
My error occured because of comment line >>> base.OnLoad(e); in Site.Master.cs
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
PEACE
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...