开发者

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

开发者 https://www.devze.com 2022-12-22 10:48 出处:网络
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...)

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 olde开发者_JAVA技巧r 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.


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.


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());


Put the generated JavaScript code into a property on the master-page, and the master-page handle it. The master-page renders after the pages it encapsulates, but it's members are available to it's content-pages.

0

精彩评论

暂无评论...
验证码 换一张
取 消