Asp. Net call front-end js call back-end code sharing, asp. netjs

Source: Internet
Author: User

[Switch] Asp. Net call frontend js call background code sharing, asp. netjs

1. C # foreground js calls background code

Front-end js

1 <script type = "text/javascript" language = "javascript"> 2 function Ceshi () 3 {4 var a = "<% = Getstr () %> "; 5 alert (a); 6} 7 </script> 8 <input type = "button" onclick = "Ceshi ();" value = "js call background code"/>

Background code

1 public string Getstr () 2 {3 string aa = "Hello! "; 4 return aa; 5}

 

2. C # Call the front-end js code in the background
Front-end js

1 <script type = "text/javascript" language = "javascript"> 2 function Ceshi () 3 {4 var a = "Hello! "5 alert (a); 6} 7 </script> 8 <asp: button ID = "Button1" runat = "server" Text = "background call js" onclick = "button#click"/>

Background code

1 protected void button#click (object sender, EventArgs e) 2 {3 // If UpdatePanel exists, call the foreground js4 ScriptManager using the following code. registerStartupScript (UpdatePanel1, this. page. getType (), "", "Ceshi ();", true); 5 // if not, Code 6 this. page. clientScript. registerStartupScript (this. page. getType (), "", "<script> Ceshi (); </script>", true); 7}

3. Execute functions in C # code in javascript Functions:

Method 1: 1. Create a button to write the called or processed content to button_click in the background;
2. Write a js function at the front end with the content of document. getElementByIdx ("btn1 "). click (); -- master page: document. getElementById ("<% = txtName. clientID %> ");
3. Call js functions on the frontend or backend to stimulate the click event, which is equivalent to accessing the c # function in the background;

Method 2:

1. function declaration is public
Background code (change public to protected)

1 public string ss()2             {3               return("a");4             }

2. You can call <% = fucntion () %> in html.

Foreground script

1 <script language=javascript>2     var a = "<%=ss()%>";3     alert(a);4 </script>

Method 3:

1 <script language = "javascript"> 2 <! --3 function _ doPostBack (eventTarget, eventArgument) 4 {5 var theForm = document. form1; // indicates the form 6 theForm of runat = server. _ EVENTTARGET. value = eventTarget; 7 theFrom. _ EVENTARGUMENT. value = eventArgument; 8 theForm. submit (); 9} 10 --> 11 </script> 12 <input id = "Button1" type = "button" name = "Button1" value = "button" onclick = "javascript: __dopostback ('button1', '')">

Method 4:

<Script language = "javascript"> function SubmitKeyClick () {if (event. keyCode = 13) {event. cancelBubble = true; event. returnValue = false; document. all. funName. value = "Name of the function you want to call"; document. form [0]. submit () ;}</script> <INPUT onkeypress = "SubmitKeyClick () "id =" aaa "type =" text "> <input type =" hidden "name =" FunName "> <〈! -- Used to store the function you want to call --> --〉

In. CS, there are:

1 public Page_OnLoad () 2 {3 if (! Page. IsPost () 4 {5 string strFunName = Request. Form ["FunName"]! = Null? Request. form ["FunName"]: ""; 6 // determines which function is called Based on the passed value. 7 switch (strFunName) 8 {9 case "enter ()": 10 enter (); // call this function 11 break; 12 case "other": 13 // call other functions 14 break; 15 default: 16 // call the default function 17 break; 18} 19} 20} 21 22 public void enter () 23 {24 //...... For example, calculate a value of 25}

 

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.