Add a Javascript script to the Asp.net page

Source: Internet
Author: User

1. Use Page. clientscript. registerclientscriptblock

The registerclientscriptblock method can place JavaScript Functions on the top of the page. That is, the script is used to start the page in the browser.

Code

<% @ Page Language = "C #" %>

<SCRIPT runat = "server">

Protected void page_load (Object sender, eventargs E)

{

String myscript = @ "function alerthello () {alert ('Hello ASP. net ');}";

Page. clientscript. registerclientscriptblock (this. GetType (),

"Myscript", myscript, true );

}

</SCRIPT>

In this example, the JavaScript function alerthello () is created as a string myscript. Then, use the page. clientscript. registerclientscriptblock method to write the script on the page. The two construction methods of the registerclientscriptblock method are as follows:

● Registerclientscriptblock (type, key, script)

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

4.4.2 use page. clientscript. registerstartupscript

Registerstartupscript is slightly different from registerclientscriptblock. The biggest difference is that registerstartupscript places the script at the bottom of the ASP. NET page, rather than the top. In fact, the registerstartup script method even uses the same constructor as the registerclientscriptblock method:

● Registerstartupscript (type, key, script)

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

So what is the difference between the script registration process on the page? In fact, there is a big difference!

If you have some JavaScript code for processing controls on the page, you should use the registerstartupscript method in most cases, instead of the registerclientscriptblock method.

That is, page. clientscript. registerstartupscript is used when page controls are searched and called.

4.4.3 use page. clientscript. registerclientscriptinclude

The last method is registerclientscriptinclude. Many developers put Javascript in. js files. This is the best way, because it is easy to apply modifications to JavaScript throughout the application. You can use the registerclientscriptinclude method to register a script file on the ASP. NET page, as shown below.

String myscript = "myjavascriptcode. js"

Page. clientscript. registerclientscriptinclude ("mykey", myscript );

 

Syntax C #

Public void registerclientscriptblock (type, string key, string script, bool addscripttags)

Parameters

The type of the client script to be registered.
Key of the client script to be registered.
The client script Text to be registered.
Addscripttags indicates whether to add a Boolean value of the script tag.

Public void registerstartupscript (type, string key, string script, bool addscripttags)

Parameters

Type indicates the type of the startup script to be registered.
Key of the startup script to be registered.
The STARTUP script text to register.
Addscripttags indicates whether to add a Boolean value of the script flag.

Public void registerstartupscript (type, string key, string script)

Parameters

Type indicates the type of the startup script to be registered.
Key of the startup script to be registered.
The STARTUP script text to register.

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.