Re: Decoupling javascript from UserControls
- From: Ole Viaud-Murat <o.viaudmurat@xxxxxxxxxxxxxx>
- Date: Mon, 20 Jul 2009 07:17:14 -0700 (PDT)
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 itWhat is the right way to accomplish these tasks?
works.
with kind regards
Ole
.
- References:
- Decoupling javascript from UserControls
- From: Ole Viaud-Murat
- Re: Decoupling javascript from UserControls
- From: Gregory A. Beamer
- Re: Decoupling javascript from UserControls
- From: Ole Viaud-Murat
- Re: Decoupling javascript from UserControls
- From: Andrew Morton
- Decoupling javascript from UserControls
- Prev by Date: Re: Deleting a record from a database
- Next by Date: Re: Decoupling javascript from UserControls
- Previous by thread: Re: Decoupling javascript from UserControls
- Next by thread: Re: Decoupling javascript from UserControls
- Index(es):
Relevant Pages
|