Two Methods for registering scripts in Asp.net

Source: Internet
Author: User

Use Page. clientscript. registerclientscriptblock

 

The registerclientscriptblock method can place a JavaScript function on the top of the page. This indicates that the script is used to start pages in the browser. Its usage is as follows:

 

Code

<% @ Page Language = " VB "   %>
< Script Runat = "Server" >
Protected sub page_load (byval sender as object, byval e as_system.eventargs)
Dim myscript as string =   " Function alerthello () {alert ('Hello _ ASP. net ');} "
Page. clientscript. registerclientscriptblock (Me. GetType (),_ " Myscript " , Myscript, true)
End sub
</ Script >
< Html Xmlns = "Http://www.w3.org/1999/xhtml"   >
< Head Runat = "Server" >  
< Title > Adding Javascript </ Title >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< Div >
< ASP: button ID = "Button1" Runat = "Server" Text = "Button" Onclientclick = "Alerthello ()"   />
</ Div >
</ Form >
</ Body >
</ Html >

 

 

Here we create the JavaScript function alerthello as a string myscript. Use the page. client script. Register clientscriptblock method to place the script on the page. The two structures of the registerclientscriptblock method are as follows:

Registerclientscriptblock (type, key, script)

Registerclientscriptblock (type, key, script, script tag Specification)

The preceding example specifies the ME type. getType, key, script to be included, followed by a Boolean value set to true, so. net will automatically put the script in ASP.. NET page. You can viewSource code, As shown below:

 

Code

< Html Xmlns = "Http://www.w3.org/1999/xhtml"   >
< Head > < Title > Adding Javascript
</ Title > </ Head >
< Body >
< Form Method = "Post" Action = "Javascriptpage. aspx" ID = "Form1" >
< Div >
< Input Type = "Hidden" Name = "_ Viewstate" Value = "/Wepdwukmty3nze5mjiymgrkiyysrmg + bcxi9diawylbxnditdo ="   />
</ Div >
< Script Type = "Text/JavaScript" >
<! -- Function Alerthello () {alert ( ' Hello ASP. NET ' );} // -->
</ Script >
< Div >
< Input Type = "Submit" Name = "Button1" Value = "Button" Onclick = "Alerthello ();" ID = "Button1"   />
</ Div >
</ Form >
</ Body >
</ Html >  

 

 

From this sectionCodeYou can see that the specified script is placed before the Page code of the ASP. NET page. Not only contains the <SCRIPT> tag, but also adds the corresponding annotation tag outside the script (so the old browser will not crash ).

 

Use Page. clientscript. registerstartupscript

The registerstartupscript method is similar to the registerclientscriptblock method. The main difference is that the registerstartup script method places the script at the bottom of the ASP. NET page instead of the top. In fact, the registerstartupscript method and the registerclientscriptblock method adopt the same structure:

Registerstartupscript (type, key, script)

Registerstartupscript (type, key, script, script tag Specification)

What makes scripts registered in different places on the page? In fact, there are many differences. If JavaScript code processes a control on the page, the registerstartupscript method should be used in most cases, instead of the registerclientscript block method. For example, use the following code to create a page that contains a simple <asp: textbox> control. The default value is Hello ASP. NET:

 

<Asp: textbox id = "textbox1" runat = "server"> Hello ASP. net </ASP: textbox> then, use the registerclientscriptblock method to place a script on the page to use the value in the textbox1 control:

Protected sub page_load (byval sender as object, byval e as system. eventargs)

Dim myscript as string = "alert (document. Forms [0] ['textbox1']. Value );"

Page. clientscript. registerclientscriptblock (Me. GetType (), "mykey", _ myscript, true)

End sub

Running this page will generate a javascript error.

 

The error occurs because the JavaScript function is triggered before the text box control is placed on the screen. Therefore, the page will throw an error if textbox1 cannot be found in the JavaScript function. Now try the registerstartupscript method:
Protected sub page_load (byval sender as object, byval e as system. eventargs)

Dim myscript as string = "alert (document. Forms [0] ['textbox1']. Value );"

Page. clientscript. registerstartupscript (Me. GetType (), "mykey", _ myscript, true)

The end sub method places JavaScript Functions at the bottom of the ASP. NET page. Therefore, when executing JavaScript Functions, The textbox1 element is found and executed as expected.

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.