Re: Decoupling javascript from UserControls

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On 19 Jul., 21:33, "Andrew Morton" <a...@xxxxxxxxxxxxxxxxxxxxxx>
wrote:
"Ole Viaud-Murat"  wrote

I guess the main problem is, that i'm using constructs like

var el = $get('<%= tbSomeControl.ClientID %>');

Probably i should try to remove those Server-Side Client-ID generation
and get those a different way.

I dont know if i understood you wrong or if my question was unclear,
perhaps you know now what i mean?

I get the impression that you want your JS to be in external .js files, yet
still be able to reference the server-generated IDs. If so, you can have
your server-side code emit a simple bit ofJavaScriptthat merely writes out
variables into a JS section before your imported JS code needs to reference
those variables, e.g.

JavaScriptSection.Text=String.Format("var inputBox='{0}';",
inputBox.ClientId)

(where JavaScriptSection is a Literal, appropriately decorated with <script
type=....> and the "//<![CDATA[" xhtml-compliance thing. Or there's some
ClientScriptManager.RegisterClientScriptBlock (q.v.) thing you can use,
which provides the correct decoration automatically.)

and then in your JS files refer to the required entity with
Document.GetElementById(inputBox).

I'm not saying that's the right way to do it, but it's a possibility, and it
works.

OTOH, and it wouldn't be the first time I'm completely wrong, there is some
call you can make from the external JS file to let the scriptmanager know
that your script is loaded - I saw it in one of those instructional videos
listed in the Start page in VS2008. Someone else please comment on this, but
ISTR you can then use some $get functionality to reference elements by their
ID as it is written the aspx page, and it will convert it to the ID emitted
by the server.

Andrew

Ok, thanks a lot to you both.

After having written my second post i realized, what i am doing. And
before all what i am doing wrong.

I now separate the js from the usercontrols and avoid ClientID
generation in the markup and set vars as you mentionend in the Render
() event by using ScriptManager.RegisterStartupScript
(). The external js files have to be finished by a call to
Sys.Application.notifyScriptLoaded(). Within those scripts i use $get
() to get a reference to the elements and it works fine. That reduced
the Response Size that's sent to the browser by almost 500 lines of js
code. and if i'm not wrong, those 500 lines are now cached.

The feature that you mentioned being able to use a simple $get() on
the markup-id would be very handy - i guess you have to register the
scripts / blocks through ScriptManager.Include("script", typeof
(control)) so it can establish the complete offset to the element.

And in almost all of the cases i can pass the id as a parameter to the
functions. Though It gets a bit annyoing when you have to reference
elements that are placed inside of FormView-Templates.

However thanks a lot for your hints and help.

I'm not saying that's the right way to do it, but it's a possibility, and it
works.
What is the right way to accomplish these tasks?

with kind regards
Ole
.



Relevant Pages

  • Re: Decoupling javascript from UserControls
    ... If so, you can have your server-side code emit a simple bit of JavaScript that merely writes out variables into a JS section before your imported JS code needs to reference those variables, e.g. ... OTOH, and it wouldn't be the first time I'm completely wrong, there is some call you can make from the external JS file to let the scriptmanager know that your script is loaded - I saw it in one of those instructional videos listed in the Start page in VS2008. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: window object
    ... Within a script, every reference to an object amounts to two ... calls from the scripting engine to the DHTML Object Model. ... var sText = document.all.div1.innerText; ...
    (comp.lang.javascript)
  • RE: Ajax calls web service in wcf(windows service)
    ... this is a "cross-browser" script restriction which prevent script to ... use AJAX call to invoke local server-side page method and let ... Move the WCF to the local site/application(at least need to have the ... restrict the script to access pages on remote server. ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Ajax calls web service in wcf(windows service)
    ... this is a "cross-browser" script restriction which prevent script to ... use AJAX call to invoke local server-side page method and let ... Move the WCF to the local site/application(at least need to have the ... restrict the script to access pages on remote server. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Client Web Browser & Remote SQL Script Error?
    ... If the server-side code path is identical, I suggest you focus on the client ... This causes your server-side script to generate an invalid SQL statement ... For SQL Authentication: ... -> I open a web browser on my local PC ...
    (comp.databases.ms-sqlserver)