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