ASP. NET in the foreground JavaScript interacts with the background C #

Source: Internet
Author: User

method One: use the AJAX development Framework, the background method definition before adding [Ajaxpro.ajaxmethod], and then you can call in the foreground JS script in the background C # function.

method Two: the background method is declared public or protected, and then the foreground uses the JS script to make the call.

 Public string  return"JavaScript function to execute the background C # method : "  <script type="text/javascript"var"<%= STR ()%>"</script>

method Three: use session variables

here is the code snippet:. CS//determines whether a session variable with the specified key value existsif(session["SiteName"] ==NULL)//Create session variable if not present, assign value to session["siteName"] variablesession["SiteName"] ="";. aspxvarSitename="<%=session["SiteName"]%>";

Method Four:

here is the code snippet:<asp:button id="Button1"runat="Server"onclick="Button1_Click"style="Display:none;"/> <input type="Button"Id="Btnsave"Value="Save"onclick="Getbehind (); return false;"/>. Aspx.cs<script type="Text/javascript">//calling the background method//using this method to invoke the background code, you can do some validation in the foreground before callingfunction Getbehind () {document.getElementById ("Button1"). Click (); } </script>. CSprotected voidButton1_Click (Objectsender, EventArgs e) {}

How to execute JavaScript functions in the background C # code

  method One:void Page.registerstartupscript (string key,string script);

  method Two: use the hidden field or literal control, use JS script in the foreground to write some JS function control value into the hidden domain or literal control, and then the foreground use Hidden.value or literal.text read the foreground value.

here is the code snippet:. aspx function Gettitleid (obj) {Stitleid=objif(stitleid!=NULL) document.getElementById ("HiddenField1"). value=type+','+Stitleid; Elsedocument.getElementById ("HiddenField1"). value=type+', 0'; }. CSstringHiddenvalue = This. Hiddenfield1.value;

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.