1. Use Page. clientscript. registerclientscriptblock
Use Page. clientscript. registerclientscriptblock to prevent javasThe TRIM function is placed at the top of the page.
For this output, note: By using registerclientscriptblock, javasThe transform function follows the Enable element <form> in HTML code.
The following is an example.
Pre-file 4-10.aspx
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "4-10.aspx.cs" inherits = "_ 4_10" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: button id = "button1" runat = "server" text = "click" onClientclick = "alerthello ()"/>
</Div>
</Form>
</Body>
</Html>
Post File 4-10.aspx.cs
Using system;
Using system. DaTa;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public partial class _ 4_10: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String myscript = @ "function alerthello () {alert ('Hello World ');}";
Page. clientscript. registerclientscriptblock (this. GetType (), "myscript", myscript, true );
}
}
What can I see when I run it ?? Click "click" to obtain an alert. But what do you see in our code? We did not write <SCRIPT type = "text/javasTermination "> </SCRIPT>. In the past, page. clientscript. registerclientscriptblock was automatically generated for us. Of course, this is used when your function is relatively large.
The above is a correct example to analyze an exception example.
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "4-10.aspx.cs" inherits = "_ 4_10" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "textbox1" runat = "server"> Hello ASP. NET </ASP: textbox>
</Div>
</Form>
</Body>
</Html>
Using system;
Using system. DaTa;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public partial class _ 4_10: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String myscript = @ "alert (document. Forms [0] ['textbox1']. Value );";
Page. clientscript. registerclientscriptblock (this. GetType (), "myscript", myscript, true );
}
}
When page. clientscriptregisterclientscriptblock takes effect, textbox is not generated yet. So there is no way to find textbox1
Modify page. clientscript. registerclientscriptblock to page. clientscript. registerstartupscript.
2. Page. clientscript. registerstartupscript ()
When you have a javasWhen the callback function is used.
The two possible structures of the registerstartupscript method are as follows:
? Registerstartupscript (type, key, script)
? Registerstartupscript (type, key, script, script tag Specification)
Page. clientscript. registerstartupscript (this. GetType (), "myscript ",
"Function alerthello () {alert ('hello, ASP. net') ;}", true );
Button1.attributes ["onClick "] =" alerthello ()";
Button2.attributes ["onClick "] =" alerthello ()";
3. Page. clientscript. registerclientscriptinclude
This is used to include JS files. We used to write a <SCRIPT type...> header in HTML, which is not needed now.
Write the following code directly on the Asp.net page. For example, we have a myjs. js file.
String myscript = "myjs. js ";
Page. clientscript. registerclientscriptinclude ("mykey", myscript );