Page. clientscript

Source: Internet
Author: User

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 );

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.