OT (just a bit) Javascript in custom controls

OT (just a bit) Javascript in custom controls

Old forum URL: forums.lhotka.net/forums/t/2955.aspx


david.wendelken posted on Wednesday, May 30, 2007

I'm bulding a Csla-aware set of web controls and have hit a technical snag.

This is a re-usable set of controls that is being built in its own project, because many different web apps might want to reference it.

The first class is DataTypeTextBox, which is a sub-class of TextBox that understands the concept of "Required" and "DataType".  It performs javascript validation on the client-side plus C# server-side validation.  It automatically includes two Validators, the RequiredFieldValidator and a CustomValidator.  The CustomValidator wants to call a javascript function that I have to write.

So far, so good.  I wrote the javascript inside the c# code by assembling it into a big string that included a <script> tag.  I had the control DataTypeTextBox control register the script and the proof-of-concept worked fine. :)

Except that I still had a bunch of javascript to write, to turn the proof-of-concept into a full-blown control.  And, having to embed each line of the javascript inside a string made the javascript harder and harder to maintain and extend.

So, I've tried moving the javascript into a .js file inside the web control project, and changing the <script> tag to include a "src=validationCode.js" attribute instead of the full javascript code listing.

This seems to have several advantages over building the javascript in each page that uses the control at runtime.  First, the browser can download the .js file once and recognize that nothing has changed, so it doesn't have to download the same javascript over and over again.  Plus, I get to write pure javascript instead of javascript embedded into strings.

However, it's not working, because it appears that the <script> tag is added to the page by the web control project, but the page belongs to a different project, a web app.  The browser then evaluates the "src=" tag and appears (quite reasonably) to assume it is talking about a directory in the web application, not the web control application. :(  So, of course, it cannot find it.

Any ideas how I can get my web control dll that's referenced by the web application to find the javascript file?  Ideally, in an "its works automagically" manner. :)

Copyright (c) Marimer LLC