How to register Javascript with asp.net

Source: Internet
Author: User


1. IsStartupScriptRegistered: indicates whether the script is enabled for the object.
2. IsClientScriptBlockRegistered: indicates whether the client script block is registered.
3. IsClientScriptIncludeRegistered: indicates whether the object is registered with a client script.
Example 1:
String info = "<script> alert ('Are you good? '); </Script> ";
// Determine whether the event is registered. Write to the region: <body >---- <script> </body>
If (! Page. ClientScript. IsStartupScriptRegistered ("test "))
{
// Dynamically register and execute Javascript
Page. ClientScript. RegisterStartupScript (this. GetType (), "test", info );
}
Example 2:
Aspx file:
Name: <input id = "txtUserName" type = "text"/>
<Input id = "btnSubmit" type = "button" value = "OK" onclick = "salert (txtUserName. value)"/>
Cs file:
String info = "function salert (userName) {alert ('your name: '+ userName )}";
// Determine whether test has been registered
If (! Page. ClientScript. IsClientScriptBlockRegistered (this. GetType (), "test "))
{
// Dynamically register Javascript
Page. ClientScript. RegisterClientScriptBlock (this. GetType (), "test", info, true );
}

Example 3:
Aspx file:
Name: <input id = "txtUserName" type = "text"/>
<Input id = "btnSubmit" type = "button" value = "OK" onclick = "salert (txtUserName. value)"/>
Jscript. js
Function salert (userName)
{
Alert ("Your name" + userName );
}
Cs file:
// Determine whether test has been registered
If (! Page. ClientScript. IsClientScriptIncludeRegistered (this. GetType (), "test "))
{
// Dynamically register Javascript
Page. ClientScript. RegisterClientScriptInclude ("test", "Jscript. js ");
}

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.