Newbie looking for explanation of why RegisterClientScriptBlock is necessary?

  • Thread starter Thread starter kbutterly
  • Start date Start date
K

kbutterly

Good afternoon, all,

I am a experienced Web programmer, trying to learn ASP.net 2.0 and I am
stuck on the necessity of the RegisterClientScrptBlock and
RegisterStartupScript methods.

According to WROX's Professional ASP.NET 2.0, "the
RegisterClientScriptBlock method allows you to place a Javascript
function at the top of the page" and "the RegisterStartupScript places
the script at the bottom of the ASP.NET page"

Why are these necessary? Why can't I just put the Javascript functions
on the top or bottom of the page myself like I've always done?

I must be missing something; the WROX book uses simple alert('hello')
functions to illustrate these two methods. Maybe their use is more
obvious with a more complex use?

I would be very appreciative if someone could point me to a good
explanation for the necessity/use of these messages.

Thanks,
Kathryn
 
If you have inline javascript (as in not included within a function) and that
script references an object in the page, that code will not work until all of
the objects in the page are rendered.

For instance, if I have an Input element named txtFirstaname and I have
javascript inline in the HEAD element or just after the FORM opening tag
that says "var s = txtFirstname.value;" it will throw an exception because
txtFirstname does not exist yet. document.getElementById("txtFirstname")
fails at this point as well.

If I have inline javascript at the end of the page, either just before the
closing FORM tag, then those two examples would work.

And therein lies the difference between RegisterClientScriptBlock and
RegisterStartupScript.

RegisterStartupScript renders at the end of the page rendering just before
the closing FORM tag. This means that all of the page's objects have been
rendered and this script can operate on them. The behavior becomes similar
to the onload script in that it runs after the page has been rendered. The
exception is that this script runs after the page is loaded because the
script, itself, is rendered after the page is rendered.

RegisterClientScriptBlock, on the other hand, is useful for functions like
form validation or other things that happen in response to "Client" input.
You can include inline code that you want to happen before the rest of the
page is rendered as well. Also include code that must have already run
before the page is rendered to support inline code that is inter-mingled with
the rendered code.

Other than code that you specifically want to run before the page is
rendered, you could safely put your validation, etc. functions in
RegisterStartupScript which would render them at the bottom of the page but I
don't recommend it. The point of this is that sometimes, when you're looking
at other people's code, you'll find all the functions at the bottom because
they didn't understand the proper use of these two methods - or they
interpreted the proper use of the two methods differently from how I
interpret it.

HTH

Dale
 
Actually, I just re-read your question and think I understand more of what
you're looking for. There are a couple benefits to using these methods
rather than just including the javascript in your HTML code.

First, by naming the script with the first parameter in the method calls,
you can prevent the same script from being included more than once.

Second, and part of how the instance of the first reason becomes important,
you can include javascripts in web server controls or user controls that you
include on a page. For instance, a common user control I developed and use
in my work recently is what I call LinkedDropdowns. Basically, this is two
dropdowns, one containing the code for a product and another containing the
product name. The user can select a product by code or by name. When one
dropdown list is changed, the other, using client-side javascript,
synchronizes with the change.

The javascript for this control is added to the page by the control itself
in codebehind by calling RegisterClientScriptBlock();

An example of when we use RegisterStartupScript() in code is when we do
server-side form validation. We set the focus to the first control with an
error using RegisterStartupScript. That way, when the javascript sets the
focus to the control, the control already exists.

My blog article at
http://www.dalepreston.com/Blog/2005/07/visually-add-client-scripts-to-your.html
shows an even more advanced use of those methods. The control described
there allows you to visually add scripts to your page in the designer and
mark them to render at the top or at the bottom. I haven't tried this in
..Net 2.0 but you can get the general idea from the article.

HTH

Dale
 
Reuse is the main reason.
As as pointed out, you can check to see if its already been registered by
giving each block a unique name.

...

If your javascript is for that page only, then you can put it at the top of
hte aspx page.
If fact, using a customvalidator with a custom javascript function is kinda
built on this premise.

You'll find in asp.net , there are several ways to do the same thing.

I started out with custom scripts, but now I use the RenderClientScriptBlock
mostly now.



...........
 
Dale,

Thanks for the explanation!

Kathryn

Actually, I just re-read your question and think I understand more of what
you're looking for. There are a couple benefits to using these methods
rather than just including the javascript in your HTML code.

First, by naming the script with the first parameter in the method calls,
you can prevent the same script from being included more than once.

Second, and part of how the instance of the first reason becomes important,
you can include javascripts in web server controls or user controls that you
include on a page. For instance, a common user control I developed and use
in my work recently is what I call LinkedDropdowns. Basically, this is two
dropdowns, one containing the code for a product and another containing the
product name. The user can select a product by code or by name. When one
dropdown list is changed, the other, using client-side javascript,
synchronizes with the change.

The javascript for this control is added to the page by the control itself
in codebehind by calling RegisterClientScriptBlock();

An example of when we use RegisterStartupScript() in code is when we do
server-side form validation. We set the focus to the first control with an
error using RegisterStartupScript. That way, when the javascript sets the
focus to the control, the control already exists.

My blog article at
http://www.dalepreston.com/Blog/2005/07/visually-add-client-scripts-to-your.html
shows an even more advanced use of those methods. The control described
there allows you to visually add scripts to your page in the designer and
mark them to render at the top or at the bottom. I haven't tried this in
.Net 2.0 but you can get the general idea from the article.

HTH

Dale
 
Sloan,

Thanks for replying!

Kathryn
Reuse is the main reason.
As as pointed out, you can check to see if its already been registered by
giving each block a unique name.

..

If your javascript is for that page only, then you can put it at the top of
hte aspx page.
If fact, using a customvalidator with a custom javascript function is kinda
built on this premise.

You'll find in asp.net , there are several ways to do the same thing.

I started out with custom scripts, but now I use the RenderClientScriptBlock
mostly now.



..........
 
Back
Top