Reputation: 2291
The master page is getting called twice. Once after the full page is rendered. At this time isPostback value is false and SESSION is null.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//SOME CODE
try
{
strUserId = Session[USERID].ToString();
}
catch
{
Response.Redirect("error.aspx");
}
}
}
For the first page load of master page (before rendering of page) everything is fine. After rendering again page_load of master page is called. This time accessing SESSION is giving an exception:
HttpException (Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the
<configuration>\<system.web>\<httpModules>
section in the application configuration.).
I dont have any problem if page_load is getting called twice or thrice. I just dont want to access SESSION on second page load.
EDIT 2
The second master page load is happening because of this code which i've overseen earlier (present Global.asax file)
void Application_Error(object sender, EventArgs e)
{
Server.Transfer("~/ErrorMessage.aspx");
}
The ErrorMessage.aspx page is the child page of master page :(
But still the answered question is why is the Application_Error() being generated even on successful rendering of pages? I tried to keep a break point at Application_Error() and event args is empty every time.
Upvotes: 2
Views: 3965
Reputation: 2291
I think I have found the reason for the problem, after going through the requests in firebug. Some ajax requests were there for two .gif files which were not present. So, the Application_Error() in the Global.asax was called. The exception generated there was "File not found". Then there was a Server.Transfer("~/ErrorMessage.aspx") in Application_Error(). Unfortunately ErrorMessage.aspx was child page of the master page. So, on every page the AJAX requests were made which caused "File not found" exception and then it was transfered to ErrorMessage.aspx, it was looking like master page page_load was called twice.
Upvotes: 1
Reputation: 67898
Try adding the following code to your Web.config
file - you may have to graft it in because I don't know what it currently looks like. You can reference this post to see the entire conversation and conditions.
<system.webServer>
<modules>
<!-- UrlRewriter code here -->
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition="" />
</modules>
</system.webServer>
The issue is very likely surrounding the fact that you're trying to wire up the event handler in the ASPX
page of the master page. This has always caused issues surrounding things like accessing Session
or even Application
variables. So, please try the following:
ASPX
file.Constructor Code
public MasterPage()
{
this.Load += new EventHandler(Page_Load);
}
And then run the code you've been running in the same handler.
Check your Web.config
and see if it has the following section setup properly.
<system.web>
<pages enableSessionState="true" />
...
</system.web
Upvotes: 2
Reputation: 3524
The error description tells you to check the settings for using Session
. Have you checked that setting (enableSessionState
)?
Also, check that error.aspx
doesn't use the master page until you know it works better, else you could get some strange errors...
Upvotes: 0