Asp. NET in the background Register JS script Introduction

Source: Internet
Author: User
Tags object net string

1. Use of Page.ClientScript.RegisterClientScriptBlock
C#
Code
1 <%@ Page language= "C #"%>
2

The results of the operation are as follows:

<br>Adding JavaScript <br>



Value= "/wepdwukmty3nze5mjiymgrkiyysrmg+bcxi9diawylbxnditdo="/>



Id= "Button1"/>




2. Use of Page.ClientScript.RegisterStartupScript
The biggest difference between the RegisterStartupScript method and the RegisterClientScriptBlock method is that RegisterStartupScript places the script at the bottom of the ASP.net page, and registerclientscriptblock the script at the top of the ASP.net page.
If you have the following code in your page:
hello asp.net

C#
protected void Page_Load (object sender, EventArgs e)
{
String myScript = @ "alert (document.forms[0][' TextBox1 '].value);";
Page.ClientScript.RegisterClientScriptBlock (this. GetType (),
"MyScript", MyScript, True);
}
This page runs with an error, because the JavaScript function is placed in the browser before text box. Therefore, the JavaScript function cannot find TextBox1.
C#
protected void Page_Load (object sender, EventArgs e)
{
String myScript = @ "alert (document.forms[0][' TextBox1 '].value);";
Page.ClientScript.RegisterStartupScript (this. GetType (),
"MyScript", MyScript, True);
}
This code places the JavaScript function at the bottom of the ASP.net page, so it can find TextBox1 when JavaScript is running.
3. Use of Page.ClientScript.RegisterClientScriptInclude
Many developers place JavaScript in the. js file, using the RegisterClientScriptInclude method to register JavaScript in the. js file.
C#
String myScript = "Myjavascriptcode.js"
Page.ClientScript.RegisterClientScriptInclude ("MyKey", myScript); This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20130707/38955.html

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.