ASP. NET Server writes JavaScript script to client

Source: Internet
Author: User
Tags array definition

Processing scripts on the ASP. ClientScriptManager, which is the Web Form server side, is generally used. ClientScript. This object is a more common method:

1, Registerarraydeclaration: On the server side, generate an array definition to the client

Service-side code: this. Clientscript.registerarraydeclaration ("Aary", "the");

Client "source file" rendering

<script type= "Text/javascript" >
<!--
var = new Array (aary);
-
</script>

2, RegisterClientScriptBlock: Write a script block to the client, this way, does not automatically add <script type= "Text/javascript" > and </script>

Service-side code: this. Clientscript.registerclientscriptblock (this. GetType (), "sblk", "var aa=1;");

Client "source file" rendering: Var aa=1;

3, RegisterClientScriptInclude: Write a <script type= "Text/javascript" on the client src=...></script>

Service-side code: this. Clientscript.registerclientscriptinclude ("JS", "http://www.baidu.com/test.js");

Client "source file" rendered: <script src= "Http://www.baidu.com/test.js" type= "Text/javascript" ></script>

4, RegisterExpandoAttribute: This method has a minimum of three parameters, that is, the client control ID, property name, property value, the client generates a value for the control property statement

Service-side code: this. Clientscript.registerexpandoattribute ("Form1", "FormType", "form");

Client "source file" rendering:

<script type= "Text/javascript" >
<!--
var Form1 = document.all? Document.all["Form1"]: document.getElementById ("Form1");
Form1.formtype = "form";
-
</script>

5, RegisterHiddenField: Automatically generate a <input type= "hidden" on the client ...

Service-side code: this. Clientscript.registerhiddenfield ("HiddenField", "I ' m a hidden");

Client "source file" rendering:

<input type= "hidden" name= "HiddenField" id= "HiddenField" value= "I ' m a hidden"/>

6, Registeronsubmitstatement: In the client form of the onsubmit event, the specified code is automatically added

Service-Side code:

This. Clientscript.registeronsubmitstatement (this. GetType (), "onsubmit1", "var onsubmit11=1;");
This. Clientscript.registeronsubmitstatement (this. GetType (), "Onsubmit2", "var onsubmit22=2;");

Client source file Rendering: First specify an event handler for the form, such as: <form id= ... onsubmit= "Javascript:return webform_submit ();"

Next, no matter how many registeronsubmitstatement statements are used on the server, a JavaScript function is generated, And the client-side script in each Registeronsubmitstatement method is connected as the statement for the event handler:

<script type= "Text/javascript" >
<!--
function Webform_onsubmit () {
var onsubmit11=1;var onsubmit22=2;
return true;
}
-
</script>

7, RegisterStartupScript: Put the developed script code before </form>, and, the method sent to the client's code before and after will not automatically add <script> and </script>

Service-side code: this. Clientscript.registerstartupscript (this. GetType (), "Teststartup", "var jjj=2");

Client "source file" rendered: Var jjj=2

ASP. NET Server writes JavaScript script to client

Related Article

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.