Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to Page_Load in ASP.NET (and a good WTF story)

Woo, I have a doozy of a problem (might even be one for the Daily WTF) and I'm hoping there's a solution. (My apologies for the long post...)

I have been working on a website that I inherited about a month ago. One of the parts I have been working on is fixing one of the controls (essentially a dynamic header bar) so that it displays additional information as requested by my users. As part of doing this project, I created a Site.master file so that I wouldn't have to recode the header bar into every single page. When I first started doing this, it seemingly worked very well. All the pages I had developed looked great and the bar updated as it should displaying the information as it should.

Well, when I dropped the Site.master (and this control) into older site pages (ones I did not specifically develop) I noticed that it looked bad on some of them, but not all of them. When I say it looked bad, basically, the control would left-align itself to the page rather than center as it should. I spent a couple hours debugging to no avail - CSS looked correct, the HTML appeared to be okay, I didn't see anything in the Javascript (although, I did miss something as I'll point out in a second), and even the old code looked correct (to the best that it could - it's not very well written). Another coworker took a look at the site and couldn't find anything at first, either.

It wasn't until I just thought to look at the rendered source code of the page (I had been working in the developer view up to this point in IE8) that it became clear what was wrong.

The original developer performs searches on many of the pages. To accomplish this, he queries the database for ALL the data and then loads them into Javascript arrays within the page so he can get access to them. This in itself is a huge problem because we're talking about thousands of items, and it obviously isn't scalable (and, yes, the site is slow). However, it finally clicked what was screwing up the Site.master - when he loads the data into the Javascript arrays, he writes out the data to the HTML upon Page_Load using numerous Response.Write(string) calls. The WTF (and what was messing me up) is that he inserts the Javascript before the DOCTYPE causing IE to go into quirks mode!

So, because I need to at least get this release out (I'll fix the real problem later), I was wondering: is there a way I can force this Javascript to be inserted elsewhere into the HTML—after the DOCTYPE at the very least? Right now, all the Response.Write() calls are being done in the Page_Load method. I just need them to be inserted later.

like image 505
JasCav Avatar asked Mar 11 '10 14:03

JasCav


2 Answers

There are a number of ways to do this, more than likely the "right" way of doing this would be to build out the string, then use the Page.ClientScript.RegisterClientScriptBlock method to register the JS, this will put it in the right place

Another alternative, that is more of the quick and dirty way would be to add an control to the page, and rather than response.write, build out the JS, then set the literal control's text property.

like image 165
Mitchel Sellers Avatar answered Sep 27 '22 16:09

Mitchel Sellers


The easiest way to replace the Response.Write calls would probably be to use a StringBuilder:

StringBuilder sb = new StringBuilder();
sb.Append("your script writes here"); // instead of Response.Write

And use the Page.ClientScript manager to put the script where it could be used. There are several methods to register the script, and each one places the script in different locations on the page, depending on the lifecycle of the current control you are in. Here is one example (already covered above):

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "yourScriptKey", sb.ToString());
like image 42
FlashXSFX Avatar answered Sep 27 '22 15:09

FlashXSFX