ASP.Net Page Sometimes Freezing Before Page Loads - asp.net

I have a pretty standard ASP.Net page written in C#. Most of the time it loads just fine and works a treat but occasionally the page won't load, I've inserted break points in the Page_PreInit (I don't actually have any code here, I've just created the function so I can see if the codes getting this far) but it's not getting this far. Any ideas where I should start in looking for a solution?

A couple of things to help you debug
Turn on tracing http://msdn.microsoft.com/en-us/library/y13fw6we%28VS.71%29.aspx
It may be throwing an exception you aren't seeing (although I would expect it to give you the YSOD...). Debug the application and turn on "Break when an exception is thrown" for Common Language Runtime Exceptions (hit CTRL+ALT+E to bring up the options)
When the page doesn't load, what happens? Does it never finish loading, do you get an error message?

Related

asp.net pages randomly lose all form data and do not postback

we've had a production application in use for about 7 years, and recently it started some strange behavior. There are a few pages where, when a user clicks a button that causes a postback and some proecessing, the browser looks like it's doing something (that is, the processing icon starts churning), but then it stops and all form data that the user enters disappears. The request never makes it to the server - I see no trace of it in the IIS logs.. no error gets written to the event log, and there are no javascript errors. It just starts and then stops. It only happens to certain users and it's not repeatable... the same user can use the same page again and it works and fails randomly. I can't reproduce the error. The browser being used is the latest Internet Explorer. I am a seasoned asp.net developer and have never seen this before.. I never ask a question in here, but this time i'm completely stumped because there is no trace of anything happening so I can't debug. Has anybody seen anything like this before?

What happened to my ASP.NET website causing NullReferenceException in code behind on controls from MasterPage?

I have the strangest problem! I have an ASP.NET website application (luckily not yet live). Yesterday and everyday before, I could run the application without error. It was working fine when I turned the computer off late last night. When I ran the application today, I got a strange message telling me that my website was offline and that I should remove the app_offline.htm page from my root directory to put it back online.
I have never seen this message or page before, so I searched online to find that apparently SQL Server can put it there temporarily while it's busy to stop any more incoming requests (or something similar). Its proper use is to stop requests going to your site when you are updrading or doing maintenance. I proceeded to remove it as suggested. I cleaned and built the solution and then tried to run it. It no longer runs.
Now, I get an error in my MasterPage code behind where I normally access the controls on the MasterPage in the Page_Load event handler (shown below).
protected void Page_Load(object sender, EventArgs e)
{
breadCrumb.BreadCrumbs = BreadCrumbs;
header.MenuItems = MenuItems;
footer.Footers = Footers;
head.Controls.Add(CreateCssLink("~/Stylesheets/main.css", "screen"));
head.Controls.Add(CreateJavaScriptLink("~/Scripts/site.js"));
if (Context.Session != null && Session["layout"] == null) Session["layout"] = "single";
if (!ArePreferencesVisible) preferences.Visible = false;
}
I want to be clear here... there are no errors in this code. There are correctly named elements in the MasterPage that correspond to those in this code. It has always worked as expected up until today and I have not changed any code since it was last working. I now get a NullReferenceException on the first line because the breadCrumb element is null. In fact, all the elements from the page that are referenced here are null.
Normally the page elements have been initialised by the time the Page_Load event is raised. What can have caused this to stop happening?
I have double checked that the MasterPage and its code behind file are still connected and they are. I also tried moving this code to the Page_LoadComplete event handler, but the controls had still not been initialised, even at that stage. I shouldn't really have to change the code because it worked just fine before.
I also commented out this code and the website ran, albeit without any styles. Interestingly, when I returned the original code to the event handler and refreshed the page, it actually worked again just as it used to. I loaded a number of different pages successfully and I thought it had fixed itself, so I restarted the application and then got the same error again. No amount of cleaning, building, changing solution configuration or restarting the application and Visual Studio make any difference.
I'm not sure if the app_offline.htm is a red herring, but I've never seen that before either so it might be connected somehow. From what I read, it seems that just removing it causes the server to restart the application domain automatically, so I can't see the connection.
I really hope someone has come across this bizarre situation before because I'm all out of ideas and I can't run my website until this is fixed.
Ok, so I finally tracked down the problem. I had an earlier backup of the website from a few days ago, so I loaded that up and it worked, albeit missing the last few days' work. I copied the latest project code into the Visual Studio > Projects folder leaving the old code in the Visual Studio > Website folder. I loaded the solution and it ran fine again.
I basically compared the Website files, updating each one in turn and periodically running the application. Eventually, the application stopped working. The last file that I had updated was a MasterPage file that extended the MasterPage that had the problem.
What I had done was comment out an empty Page_Load event handler in the MasterPage. I don't understand why that would cause this problem, so any comments explaining that would be helpful and appreciated.
This MasterPage is only used on the home page and a few others, so that explains why I didn't come across the problem the night that I changed it. (I hadn't viewed any pages that used that MasterPage after making the change). I wish I had because it would have been much easier to track down then, but such is life.
So I replaced all of my latest code, added the empty Page_Load event handler in the MasterPage again and finally, everything is working again.

Most elements of an ASP.NET page are not rendered after random idle time

We have been trying to figure this one out for a while now, without any luck.
The symptoms are as follows:
After some idle time of a specific ASP.NET 2.0 application (can be from several hours to days), one of the pages in my application stops working.
When viewing the source of the page I see many elements missing, elements that are usually there, such as: reference to WebResource.axd, the __doPostBack() function, all of the UserControls and more.
A reference to ScriptResource.axd, and the __VIEWSTATE are there.
After recycling the app pool, the application starts working correctly again and everything renders well.
This only happens on a specific server, when deploying the same application on a different server, this error does not occur.
The page that the error occurs on has only one UserControl which is not rendered when the error occurs. Nothing special happens when this page is loaded.
We tried doing periodic client refreshes, but that did not help either.
Thanks in advance.
It was a caching issue. There was a part in the code that hid specific controls when the cache was invalid. That explains the missing code parts.
I am still not sure why the cache was invalid on that specific server.

'System.Web.HttpContext.Current.Session' is null

I have an ASP.NET website that has been deployed since 2008 with plenty of users. From time to time, I've made updates to the site and uploaded those changes to the server without problems.
However, today, I'm trying to run the site on my development environment and I keep getting errors any time the code tries to access session state. It appears System.Web.HttpContext.Current.Session is always null!
Any suggestions on where to look? It's been a while since I worked on it and am not 100% sure if I've worked on it with the current version of ASP.NET (4.0).
Other than that, the code's been working fine and I haven't made any changes since it was last working.
More Information
I've spent all morning on this but I apparently have a larger issue.
If I step through the code, I see that my page Load handler executes. At this point, Session is not null. After that, my specialized master page executes. After that, my general master page executes. I then step through load events for a couple of controls. This all seems very normal.
Then, if I keep executing, suddenly I'm loading a specialized master page for another page and Session is now null!
If I hit F5, it the original page shows. But somehow it is causing the other page to load and without session state. If I turn off debug mode, it appears to run normally.
Obviously, I have something strange happening. I need to determine why the other page is being loaded.
Try to put a static page, something like Test.aspx and browse to that page. If it gets loaded, try to use Session property of the page in code behind. Do you still get the error? In that case, Session doesn't load. I suggest creating an HTTP Module and hooking into a method which is responsible for loading Session info. See what's wrong.
Just a guess - did you deactivate sessionState for any reason? Anything like this:
<sessionState mode="Off"/>
My apologies. The information I provided was completely misguided and was not sufficient to resolve the issue.
The problem was actually due to some custom error handling that redirected to an error page. This was configured in web.config. This error page was using the master page that my code was mysteriously executing.
Apparently, an error was occurring within the GridView control. This is ASP.NET code and not my own, so I was unable to step through it or catch it with a regular handler.
This was hard to understand. If I was executing the Load event handler of my error page, then it would've been obvious. But it appears to have skipped over that. Thus, my confusion.
Try checking the global.asax file or any other pre-load events and make sure the Session is not set to NULL explicitly.
Maybe your erring master page is calling code from some external class where the session object is not available?

Javascript error : " 'Sys' is undefined "

I keep having an error when running my web application. The error does not cause a compilation error when on live server at least a javascript error and nothing else. But the real problem is when "debug" ... javascript error stops the compilation and I have to "Continue" three times before proceeding normally my debug. But this error occurs at every refresh the page. All this using Visual Studio.
After several hours of search on google, I saw that it was a problem with the ScriptManager and Ajax. The real problem is that I do not use any Ajax on this page but the ScriptManager is on the masterpage. Worse still, on any other page on the website, that may use Ajax or not, no javascript error! Only THIS page cause this error! Any suggestion?
Note that I usualy talk french so there's probably error and sorry for this!
EDIT
There's the 3 places were compilation stop.
1. Sys.WebForms.PageRequestManager._initialize('ctl00$ctl08', document.getElementById('aspnetForm'));
2. Sys.WebForms.PageRequestManager.getInstance()._updateControls([], [], [], 90);
3. Sys.Application.initialize();
Make sure you include the asp.net-ajax script manager control on your pages.
Update:
From the comment:
But ONE page which do not use Ajax or anything relativly to the scriptmanager cause this error.
Wrong. The scriptmanager control causes a bunch of javascript to be included (<link ..>-ed) in your rendered html page. Without that scriptmanager control, this javascript won't be available. Among the linked javascript is the Sys object that your error is complaining about. The only way you get the Sys is undefined error is if you have javascript for your page that in some way tries to use the Sys object (perhaps via a third part control?). So I promise you, you are using "Ajax or [something related]."
However, you said you included a scriptmanager on your master page, and that should have been good enough. The most common reasons for this to be missing are that your scriptmanager control is missing or that you execute javascript that needs it before the javascript engine has imported it. So check again and make sure 1) that the page in question isn't somehow "special" in that it doesn't use the master page, and 2) that you don't have javascript running before the asp.net ajax scripts are imported.
This isn't an answer to your question, more of a suggestion for the future. I think Joel did a fine job of answering the question anyway.
For your sanity, get rid of that Microsoft AJAX. It's crap & it's not real AJAX anyway 'cause you're sending back the whole page anyway. It's just asynchronous.
Those meaningless & unnecessary "Sys is undefined" errors are part of the reason I abandoned Microsoft AJAX.
Move on to jQuery & MVC. It makes development fun again.

Resources