1. RegisterClientScriptBlock
Javascript function on the top of the page
code show as below:
protected
void
Page_Load(
object
sender, EventArgs e)
{
string myScript = @" function AlertHello() {
var oText = document.getElementById('TextBox1');
alert(oText.value); } " ;
Page.ClientScript.RegisterClientScriptBlock( this .GetType(),
" MyScript " , myScript, true );
}
{
string myScript = @" function AlertHello() {
var oText = document.getElementById('TextBox1');
alert(oText.value); } " ;
Page.ClientScript.RegisterClientScriptBlock( this .GetType(),
" MyScript " , myScript, true );
}
Html code generated as follows:
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
...
< body >
< form >
...
< script type ="text/javascript" >
// <![CDATA[
function AlertHello() {
var oText = document.getElementById ( ' TextBox1 ' );
alert(oText.value); } // ]]>
</ script >
< input name ="TextBox1" type ="text" id ="TextBox1" />
< input type ="submit" name ="Button1" value ="Button" onclick ="AlertHello();" id ="Button1" />
...
</ form >
</ body >
</ html >
...
< body >
< form >
...
< script type ="text/javascript" >
// <![CDATA[
function AlertHello() {
var oText = document.getElementById ( ' TextBox1 ' );
alert(oText.value); } // ]]>
</ script >
< input name ="TextBox1" type ="text" id ="TextBox1" />
< input type ="submit" name ="Button1" value ="Button" onclick ="AlertHello();" id ="Button1" />
...
</ form >
</ body >
</ html >
2. RegisterStartupScript
Javascript function on the bottom of the page
code show as below:
protected
void
Page_Load(
object
sender, EventArgs e)
{
string myScript = @" document.getElementById('TextBox1').value = 'Hello ASP.NET.';
var oText = document.getElementById('TextBox1');
alert(oText.value); " ;
Page.ClientScript.RegisterStartupScript( this .GetType(),
" MyScript " , myScript, true );
}
{
string myScript = @" document.getElementById('TextBox1').value = 'Hello ASP.NET.';
var oText = document.getElementById('TextBox1');
alert(oText.value); " ;
Page.ClientScript.RegisterStartupScript( this .GetType(),
" MyScript " , myScript, true );
}
Html code generated as follows:
<
html
>
...
< body >
< form >
...
< input name ="TextBox1" type ="text" id ="TextBox1" />
< script type ="text/javascript" >
// <![CDATA[
document.getElementById( ' TextBox1 ' ).value = ' Hello ASP.NET. ' ;
var oText = document.getElementById( ' TextBox1 ' );
alert(oText.value); // ]]>
</ script >
</ form >
</ body >
</ html >
...
< body >
< form >
...
< input name ="TextBox1" type ="text" id ="TextBox1" />
< script type ="text/javascript" >
// <![CDATA[
document.getElementById( ' TextBox1 ' ).value = ' Hello ASP.NET. ' ;
var oText = document.getElementById( ' TextBox1 ' );
alert(oText.value); // ]]>
</ script >
</ form >
</ body >
</ html >
3. RegisterClientScriptInclude
Javascript registered external script file
code show as below:
protected
void
Page_Load(
object
sender, EventArgs e)
{
string myScript = " ../JS/myJavaScriptCode.js " ;
Page.ClientScript.RegisterClientScriptInclude( " MyScript " , myScript);
}
{
string myScript = " ../JS/myJavaScriptCode.js " ;
Page.ClientScript.RegisterClientScriptInclude( " MyScript " , myScript);
}
Javascript code is as follows:
function
AlertHello() {
var oText = document.getElementById( ' TextBox1 ' );
alert(oText.value);
}
var oText = document.getElementById( ' TextBox1 ' );
alert(oText.value);
}
HTML call as follows:
<asp:Button ID="Button1" Runat="server" Text="Button"
OnClientClick="AlertHello()" />
It is also the script script at the bottom Code
Reproduced in: https: //www.cnblogs.com/davidgu/archive/2011/05/22/2053711.html