How to Write Javascript script content to pages in ASP. NET

Source: Internet
Author: User

The Ajax method is used to write JavaScript scripts to pages in the post code (. CS file,

Although response. Write ("<SCRIPT> script content </SCRIPT>"); you can also write script code to the page,

However, the content output by the script is mostly at the top of the page. You can use the following method to write the script content anywhere you want.

 

Clientscriptmanager cs = This. clientscript;
CS. registerarraydeclaration ("hello", "1, 2, 3 ");
CS. registerclientscriptblock (this. GetType (), "helloworld", "function helloworld () {alert (1) ;}", true );
CS. registerclientscriptinclude ("helloworld", "helloworld. js ");
CS. registerexpandoattribute (this. button1.clientid, "hello", "world ");
CS. registerhiddenfield ("hello", "world ");
CS. registeronsubmitstatement (this. GetType (), "helloworld", "Return window. Confirm ('Do you really want to submit the form? ')");
CS. registerstartupscript (this. GetType (), "helloworld", "<SCRIPT> alert ('the page has loaded! ') </SCRIPT> ");

       

   The above is the usage of script registration. The following is a note:

       // RegisterarraydeclarationEffect is to define an array
       // --------- The generated script is: var Hello =New array (1, 2, 3 );
       // RegisterclientscriptblocK results in a function, followed by the form tag in the source code
       // --------- Generated Script: function helloworld () {alert (1 );}
       // RegisterclientscriptinclUde references external JS files
       // --------- The generated script is: <SCRIPT src = "helloworld. js" type = "text/JavaScript"> </SCRIPT>
       // Registerhiddenfield generates a hidden control named "Hello World ".
       // --------- The generated script is: <input type = "hidden" name = "hello" id = "hello" value = "world"/>
       // RegisteronsubmitstatemenT
       // --------- The generated script is: function webform_onsubmit () {return window. Confirm ('Do you really want to submit the form? '); Return true ;}
       // Registerstartupscript generates a function, and the position follows the ending form tag.
       // --------- The generated script is: <SCRIPT> alert ('the page has loaded! ') </SCRIPT>

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.