1. Use Page. clientscript. registerclientscriptblock
Use Page. clientscript. registerclientscriptblock to prevent javascript functions from being placed on the top of the page.
For this output, note: By usingRegisterclientscriptblock, JavaScript Functions keep up with enabling elements in HTML code<Form>.
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 found that we did not end the program by writing <SCRIPT type = "text/JavaScript"> </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
At this time, you just need to modify it.Page. clientscript. registerclientscriptblock is changed to page. clientscript. registerstartupscript
2. Page. clientscript. registerstartupscript ()
When you have a JavaScript function that you want to start during page loading.
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 );