Page.RegisterClientScriptBlock ,Page.RegisterStartupScript and javascript

9,336 views
Skip to first unread message

crazy

unread,
Jun 28, 2007, 4:29:12 AM6/28/07
to DotNetDe...@googlegroups.com

hi all,
    I am using  VS2003.
 
I would like to know the main difference between
    Page.RegisterClientScriptBlock  and Page.RegisterStartupScript???
 
my another doubt is, if user disable javascript in his browser can we invoke a javscript???
 
i have some javscript and i save it in a JS file.
for example RoundVal().
i want to invoke his function for rounding a textbox value on its blur event,even client disable javascript in his browser??
--
"People who never make mistakes, never do anything."

Crazy

Jamie Fraser

unread,
Jun 28, 2007, 4:33:11 AM6/28/07
to DotNetDe...@googlegroups.com
If the user disables JavaScript, you can't use JavaScript. It's quite simple!

You shouldn't rely on Javascript for critical functionality if you
think the user will have it disabled, JS should add to the experience
but not prevent the user from using your site if they don't have it
enabled(depending on your way of thinking).

Look at some big AJAX-enabled sites for example, they should fall back
to the standard postback model if JS is disabled.

-Jamie

crazy

unread,
Jun 28, 2007, 6:53:14 AM6/28/07
to DotNetDe...@googlegroups.com
thank u.
ok i agree. 
 
Crazy

Cerebrus

unread,
Jun 28, 2007, 8:20:10 AM6/28/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
On Jun 28, 1:29 pm, crazy <crazysan...@gmail.com> wrote:
> I would like to know the main difference between
> Page.RegisterClientScriptBlock and Page.RegisterStartupScript???

This got left unanswered, and it's a VFAQ, so I'll add my comments :

Let's say we have a .aspx page with the following form tag : (Line
nos. are for reference)

1. <form id="Form1" runat="server">
2. ..
3. ..
4. ..
5. </form>

Now let's look at key differences for each method :

A.
Page.RegisterClientScriptBlock() will insert the *block* of script
before Line 2.
Page.RegisterStartupScript() will insert the script after Line 4.

B.
Page.RegisterClientScriptBlock() should usually be used for scripts
encapsulated in functions. (hence the word "block")
Page.RegisterStartupScript() can be used for any script, even if it's
not in a function.

C.
Page.RegisterClientScriptBlock() should be used for functions that
don't need to run on Page load.
Page.RegisterStartupScript() should be used for scripts that must run
on Page Load.

D.
Page.RegisterClientScriptBlock() should be used for a script that does
not require the form elements to have been created.
Page.RegisterStartupScript() should be used for scripts that require
the form elements to have been created and uses references to them.

Notice that all the 4 differences are essentially related to each
other (they build upon the prev. one). The difference put in one line
can sometimes be too subtle, so I separated it out...

Disclaimer : This is written from my own experience on their usage and
I was initially quite confused about them too, so if I'm mistaken,
someone please correct me.

crazy

unread,
Jun 28, 2007, 9:30:05 AM6/28/07
to DotNetDe...@googlegroups.com
thank u for the valuable information..but i didnt get point D

Raghupathi Kamuni

unread,
Jun 28, 2007, 8:51:25 AM6/28/07
to DotNetDe...@googlegroups.com
http://msdn2.microsoft.com/en-us/library/aa479390.aspx

Raghu

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This e-mail (and any attachments) are confidential and intended solely for the use of the individual or entity to whom they are addressed. While we take every reasonable precaution to screen out computer viruses from emails, including sweeping this message by MIME Sweeper version 5.2, attachments to this email may contain such viruses. We cannot accept liability for loss or damage resulting from such viruses. The integrity of email across the Internet cannot be guaranteed and Financial Objects plc will not accept liability for any claims arising as a result of the use of this medium for transmissions by or to Financial Objects. It may contain personal views which are not the views of Financial Objects unless specifically stated. If you have received it in error, please delete it from your system, do not use, copy or disclose the information in any way nor act in reliance on it and notify the sender immediately. Please note that Financial Objects monitors e-mails sent and received. If you have received this email in error please notify Financial Objects plc. Contact details can be found on our website. World Wide Web: http://www.finobj.com.
Information: Financial Objects plc is a public company, registered in England and Wales, registered number 03268748, registered office: 45 Monmouth Street, Covent Garden, London, WC2H 9DG.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com

Cerebrus

unread,
Jun 28, 2007, 10:00:44 AM6/28/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Lol, I give up !!

crazy

unread,
Jun 28, 2007, 11:01:37 AM6/28/07
to DotNetDe...@googlegroups.com
sorry .. no problem

On 6/28/07, Cerebrus <zor...@sify.com> wrote:

Cerebrus

unread,
Jun 28, 2007, 1:17:12 PM6/28/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Oh, point "D" ! I thought you meant that you didn't get the point of
the whole post. ":D"

Well, as the page loads, it's elements are created in sequence as you
declare them. So, if you have a script that refers to an element
before it has been created in the Object model of the page, an error
will result. This is why RegisterStartupScript is declared at the end
of the form tag (all form elements have been created and can be
accessed).

Hope that helps. ;-)

On Jun 28, 8:01 pm, crazy <crazysan...@gmail.com> wrote:
> sorry .. no problem
>

crazy

unread,
Jul 1, 2007, 8:07:30 AM7/1/07
to DotNetDe...@googlegroups.com
yes thanks

On 6/28/07, Cerebrus <zor...@sify.com> wrote:
Reply all
Reply to author
Forward
0 new messages