Scott on Writing

Musings on technical writing...

Programmatically Determining if WebUIValidation.js is Included in a Page?

I was browsing through the microsoft.public.dotnet.framework.aspnet.webcontrols newsgroup tonight and stumbled upon a post by a fellow named Haz, who asked:

I am building a web custom control and I need to make sure that the file
/aspnet_client/system_web/{framework version}/WebUIValidation.js
is included in the page. 

Is there any way to force the page to make sure that it is included?

My understanding is that Haz is wanting to be able to programmatically determine from his custom control whether or not the page has the client-side, JavaScript include in there for the WebUIValidation.js file.  I assume he is wanting to have his control's markup somehow interact with this file, so he needs to determine if it's been added or not.

The WebUIValidation.js file, as you know, contains client-side JavaScript functions for client-side validation.  It is automatically injected into a page when:

  1. The page contains one or more validation Web controls.
  2. The page is being visited by an “uplevel” browser.

I fired up Reflector to sneek a peak at the System.Web.UI.WebControls.BaseValidator class, which is the class from which all validation Web controls are derived.  Initially I looked at the RegisterValidationCommonScript() method, which is responsible for squirting out the JavaScript include file reference.  As I've waxed on about in my article Working with Client-Side Script, the Page class contains assorted methods for injecting client-side script.  When adding such script, you have to give it a key; there are corresponding methods to determine whether or not a script block with a specified key name has been added.

My initial though, then, was to simply check if a script block with key ValidatorIncludeScript had been registered (as this is the key name the BaseValidator uses when adding the JavaScript include reference).  However, the RegisterValidationCommonScript() method, from which this script is injected, doesn't happen until the PreRender stage of the Page's lifecycle.  If you can afford to wait this long, then this is the definitive approach.  To Haz I say this: in your control, you could override the OnPreRender() method, check to see if ValidatorIncludeScript has been added, and, if not, add it yourself.  (You can get the necessary code straight out of the BaseValidator class; in fact, I'd recommend you just copy and paste the code for the RegisterValidationCommonScript() method into your control's OnPreRender() method.

But what if you need to determine if the WebUIValidation.js script will be added?  That is, what if you need to know in, say, the Load event if this script will be added.  Well, the Page class has a public Validator property, which is a collection of the validators on the page.  The BaseValidator class has a method definition for the OnInit() method, which runs during the Initialization stage.  In this method, the validator control adds itself to the Page's Validator collection.  So, you can simply check if Page.Validator.Count > 0 - if so, there are one or more validators on the page and the WebUIValidation.js script will be emitted. ... Right?

Well, maybe not.  Remember that the the WebUIValidation.js script is emitted if there are any validators on the page and if the browser visiting the page is an uplevel browser.  Huh.  The question, then, is how do validators determine if a browser is uplevel or not?  As I discuss in Client-Side Validation in DownLevel Browsers, the BaseValidator class has a DetermineRenderUplevel() method that does the following check: a browser is considered “uplevel” if it has an MS DOM version of 4 or greater. 

Note: By default, ASP.NET only consideres IE 4.0+ uplevel; however, you can enhance your Web application and specify that other browsers (FireFox, Mozilla, etc.) should be considered uplevel.  The problem here - as I discuss in Client-Side Validation in DownLevel Browsers - is that the actual script emitted by WebUIValidation.js is non-standards compliant, and only works with IE... Bummer, eh?  There are work-arounds, as I discuss in my article.

So, if you need to know prior to the PreRender stage whether or not the WebUIValidation.js script will be emitted, you can determine if it will if both Page.Validators.Count > 0 and Request.Browser.MSDomVersion.Major >= 4.

When digging around, one thing concerned me: what if I add a dynamic validation control - what happens then?  Is the Page.Validator collection updated accordingly?  The answer is, Yes.  Whenever you add a control to another control's Controls property, it takes the added control through the Initialization and LoadViewState stages, at minimum, and through the Load stage as well, if the parent control is past the Load stage itself.  So, Page.Validators will be updated accordingly, but the one caveat is that if you are checking at, say, the custom control's Load stage, and another control in the hierarchy later adds a validator dynamically, the first control will “miss” that the WebUIValidation.js script will be emitted.  (I hope that doesn't sound too confusing.)

posted on Wednesday, November 03, 2004 9:19 PM

Feedback

# re: Programmatically Determining if WebUIValidation.js is Included in a Page? 11/4/2004 3:33 PM Haz

Thanks a lot for looking into this.
When I was looking for a solution to this issue, I came across with another project which has a similar approach as the one you recommended (probably it is for Framework 1.0)
http://www.codeproject.com/aspnet/datecontrol.asp
And I just could not find any other way of doing this.

I had to derive a new class from the BaseValidator class, add a new method to it so that I could call the protected RegisterValidatorCommonScript function.

In your post, you are saying that I could copy over the code from the baseValidator class, and just paste it into my project, but I do not have the Framework Source Code, the question is, is it available somewhere (just like MFC used to be), coz I don't have it in my VS.Net2003 Install set.

# re: Programmatically Determining if WebUIValidation.js is Included in a Page? 11/4/2004 3:38 PM Scott Mitchell

Haz, you can decompile the Framework DLLs using Reflector - http://www.aisto.com/roeder/dotnet/

See this article: http://aspnet.4guysfromrolla.com/articles/080404-1.aspx

# OTC Links For November 6 11/6/2004 5:45 PM OdeToCode Link Blog

# re: Programmatically Determining if WebUIValidation.js is Included in a Page? 12/1/2004 4:13 PM Roch

Thanks for the info. It helped me to get out of big trouble.

RN

# re: Programmatically Determining if WebUIValidation.js is Included in a Page? 2/22/2005 6:28 PM eduardo.padilla@diztek.com

Very good!

I am adding validators in a non-standard way. The page is not aware of it because I build the HTML in a string and then use a Label to render it.

# re: Programmatically Determining if WebUIValidation.js is Included in a Page? 5/14/2005 1:27 PM Phil C

Scott,
I was trying in vain to use Venkman to debug the WebUIValidation.js that I modified according to an asp.net forum post on the issue of suppressing validators on a page that has more than one submit button (A page where you login or register for a new account with seperate validators for each group).
I finally figured from your blog that iis doesn't therefore send the WebUIValidation.js
so I can't use it. Since this issue has been I believe fixed in Whidbey beta 2 and my web host is now supporting go live for it, I suppose I should shift over to Whidbey instead of banging my head against the wall!!

cc: Scott by 'contact' link

Phil

# re: Programmatically Determining if WebUIValidation.js is Included in a Page? 8/17/2005 1:38 PM Miguel

What about this message?

Unable to find the script library...WebUIValidation.js...Try to place it manually or by aspnet_regiis -c

What is it?

# re: Programmatically Determining if WebUIValidation.js is Included in a Page? 11/6/2006 12:32 PM Ben

Seems like the Page.Validators.Count > 0 approach is also problematic when you have a page full of custom validators that do not emit any client-side JS. I ran into a page that only contained server-side custom validators--even though the Page.Validators.Count returned the correct validator count, the functions from WebUIValidation.js were nowhere to be seen on the page.

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles