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;