Summary of client scripts in ASP. NET 2.0

Source: Internet
Author: User

1. Use the onclientclick attribute to open the client script. Use the following method:

JS script:

<Script language = "JavaScript" type = "text/JavaScript">
<! --
Function opensite ()
{
Window. Open ("http://www.cnblogs.com ");
Return true;
}

// -->
</SCRIPT>

Call method:

<Asp: button id = "button1" onclientclick = "opensite ()" runat = "server" text = "open site"/>

2. register the JS script dynamically. The following describes the meanings and usage of the following four methods:

The following methods are derived from the clientscriptmanager class. There are dozens of methods in the study, and some are overloaded. Let's talk about the most common methods.

1) registerclientscriptblock method: add the JS block to the top of the page. You can create the JS Code in the form of a string and pass it to the webpage adding method. Its corresponding Inclusion Method isclientscriptblockregistered ()

The usage is as follows:

// Dynamically create a JS File
String info = "";
Info + = "function showname (username )";
Info + = "{alert ('your name is: '+ username );}";

// Determine if myname has been registered
If (! Page. clientscript. isclientscriptblockregistered (this. GetType (), "myname "))
{
// Dynamically register JS
Page. clientscript. registerclientscriptblock (this. GetType (), "myname", info, true );

}

Note: You can check what code is used to generate the page?

2) registerclientscriptinclude method: Add reference JS scripts externally. Its corresponding Inclusion Method isclientscriptincluderegistered ()

Assume that there is such an info. js script as follows:

Function showcity (city ){

Alert ("My love me:" + city );

}

Example of referencing it:

// Dynamically register the Javascript file of info. js
// Determine whether the account has been registered
If (! Clientscript. isclientscriptincluderegistered (this. GetType (), "mymessage "))
{

// If it is not registered, just register info. js dynamically
Page. clientscript. registerclientscriptinclude ("mymessage", "JS/info. js ");


}

Pay attention to the parameters of each method.

3) registerstartupscript method: add the JS block when the webpage is loaded, but it will be executed before the onload event of the webpage is triggered. The corresponding include method isstartupscriptregistered.

An example of using it is as follows:

String info = "<script language = \" javascript \ "> alert (\" Hello DOTNET! \ ") </SCRIPT> ";
If (! Page. clientscript. isstartupscriptregistered ("welcome "))
{
Page. clientscript. registerstartupscript (this. GetType (), "welcome", Info );

}

4) registeronsubmitstatement method: Add JS that is executed to respond to the onsubmit event on the webpage. Its corresponding include method isonsubmitstatementregistered.

An example of using it is as follows:

String info = "Return confirm ('Are you sure you want to delete? ');";
If (! Page. clientscript. isonsubmitstatementregistered (this. GetType (), "mypage "))
{
Page. clientscript. registeronsubmitstatement (this. GetType (), "mypage", Info );

}

Note that you do not need to add the <SCRIPT> tag here, because the system has generated the tag for you.

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.