Making sure JavaScript gets called after an AJAX Partial Postback in ASP.NET

ScriptManager.RegisterStartupScript(Page page, Type type, string key, string script, bool addScriptTags)

Notice the first parameter of this method, as it has an overload that takes a Control as its first parameter.  When you use the Control overload, it only registers the startup script once on the initial rendering of the page.

If you use the Page overload listed above, it will register it for every asynchronous postback to ensure your JavaScript is executed everytime.

Remember to use ScriptManager and not ClientScriptManager...

From the Remarks on the MSDN article for the overload listed above:
"You use this method to register a startup script block that is included every time that an asynchronous postback occurs. To register a script block for a control that is inside an UpdatePanel control so that the script block is registered only when the UpdatePanel control is updated, use the RegisterStartupScript(Control, Type, String, String, Boolean) overload of this method.

If you want to register a startup script that does not pertain to partial-page updates, and if you want to register the script only one time during initial page rendering, use the RegisterStartupScript method of the ClientScriptManager class. You can get a reference to the ClientScriptManager object from the ClientScript property of the page."

Also very important:
Make sure you have the following using statement in your codebehind...
using System.Web.UI;

If you don't, you will get the error:
Cannot access internal property 'ScriptManager' here


Having Two Consecutive Hyphens in XHTML Comments Causes Anomalies in Firefox

Check out my experiment page to see the different side effects this issue causes.

If you're seeing weirdness when having hyphens in your (X)HTML comments, it's most likely because you're using two adjacent to each other, which is a big no-no according to the W3C.

Avoid using hyphens in your comments to avoid the issues... dumb, but required.

I know we're all prone to yell at Mozilla and tell them they're doing it wrong, because all the other latest versions of other browsers will still honor the comments, but Firefox isn't necessarily doing it wrong, because as I understand it, they're allowed by the standard to not honor the comment if there are two adjacent hyphens together.  In one way I think it's good that Firefox doesn't, because it forces we, the web authors, to follow standards.

On the other hand, I think the standard is flawed and that nothing between the start and end of a comment should matter... so part of me wishes Firefox would do like the other browsers and still honor the comments.

Oh well, go the safe route and keep hyphens out of your comments.


Visual Studio 2008 Crashes When You Stop Debugging After Rearranging Windows

I don't know what caused it... but somehow I managed to get Visual Studio 2008 in a state where it would crash if I tried rearranging debug and properties windows, etc and then later stopped the debugger.  Not only did it crash, but the app hung and would only close if I opened Task Manager and killed the devenv.exe process.

I started hunting and found this hotfix on MSDN, but then noticed in the comments a nicer solution to try before applying the hotfix.  The solution is simple:
  1. Start debugging
  2. Window >> Reset Window Layout
  3. Stop debugging
This worked great for me... your mileage may vary.  If it doesn't work, try downloading the hotfix.


Introducing document.queryString for JavaScript

I got sick of not having a quick/easy reference object in JavaScript for getting a query string parameter's value.  JavaScript has always offered the window.location.search property to get the entire query string, but who wants to parse that every time?

So I set out to see how long it would take me to make a quick parser that would load it into a nice object that I could jam on the document.

Roughly 30 minutes later, I had one.  It also handles param names that contain periods (person.firstname) which is nice for those that do that with their ASP.NET MVC projects.

I don't have any docs yet, but see the quick write up at my JavaScript Jedi website if you'd like to see it in action and perhaps try it for yourself.



Syncfusion MVC Grid - Prevent "A potentially dangerous Request.Form value was detected from the client" error

While working on an ASP.NET MVC project, using the Syncfusion Essential Grid for MVC, we had two pages utilizing the grid.  One page worked fine for displaying the data and paging/sorting.

The other page would show the results, but would fail on the AJAX calls back for paging/sorting.  It baffled me for several hours.  I compared all the settings between the two pages and I couldn't detect any differences between how each was set up.

I finally got frustrated and launched the site in Firefox, using Firebug to monitor the requests.
Props to Firebug, as it was the key to helping me find the solution; as AJAX calls don't show you ASP.NET error HTML when a request breaks... Firebug showed me.  The result was enlightening:

"A potentially dangerous Request.Form value was detected from the client..."
Interesting.  Do you see the "br"eaking issue here?

That's right... ASP.NET does request validation on all requests by default, to ensure prevention of cross-site scripting (See the MSDN article "How To: Prevent Cross-Site Scripting in ASP.NET" for more explanation.)

We were adding the tags into the HeaderText() calls on the cells in the grid setup on columns where the data in that column was guaranteed shorter than the header text, so we could avoid growing the table unnecessarily wide.

As soon as I removed those tags, it worked like a champ.

So be warned, don't put any HTML tags in your HeaderText calls, or you'll be bitten the same.  I hope this saves someone else several hours of pain.  I know it will save me in the future when I run into other Syncfusion grids in our app that are mysteriously not performing their AJAX calls correctly.  That'll be the first thing I look at.

(And did I mention that Firebug rocks?!)