The method of executing the c function in javascript

  • 2020-06-03 08:05:37
  • OfStack

1 How do I access the C# function in JavaScript?

2. How to access the C# variable in JavaScript?

3. How do I access the existing variables of JavaScript in C#?

How do I access the JavaScript function in C#?

The answer to question 1 is as follows:

javaScript function to execute a function in C# code:

Method 1:1. First, a button is established to write the content called or processed into button_click in the background;

2. Write an js function in the foreground, which is document. getElementById("btn1").click();

3. Call js function in the foreground or background to excite click event, equal to access c# function in the background;

Method 2:1, the function is declared as public

The following are the quotes:
Background code (changing public to protected also works)

public string ss() 

2. It can be called in html

The front desk script

<script language=javascript> 
var a = "<%=ss()%>"; 

Method 3:1,

<script language="javascript"> 
function __doPostBack(eventTarget, eventArgument) 
var theForm = document.Form1; // Refers to the runat=server the form 
theForm.__EVENTTARGET.value = eventTarget; 
theFrom.__EVENTARGUMENT.value = eventArgument; 
<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=" The name of the function you want to call " ;  
<INPUT onkeypress="SubmitKeyClick()" id="aaa" type="text"> 
<input type="hidden" name="FunName">    


The following are the quotes:

public Page_OnLoad() 
if (!Page.IsPost()) 
string strFunName=Request.Form["FunName"]!=null?Request.Form["FunName"]:""; 
// Which function to call depends on the value returned  
case "enter()": 
enter() ; // Call this function  
case " other ": 
// Call another function  
// Calling the default function  
public void enter() 
// ... Let's say I compute something  

Question 2. How do I access the C# variable in JavaScript?

The answer is as follows:

Method 1:1, through the page hidden domain access

<input id="xx" type="hidden" runat="server"> 

Method 2:1. For example, PUBLIC STRING N was defined in the background. This variable is referenced in foreground js in the form "" or "++"

Method 3:1, or you can register 1 section of the script on the page after the server-side variable assignment

"<script language='javascript'>var temp=" + tmp + "</script>" 

tmp is the background variable, and then temp can be accessed directly to get the value.

Question 3. How can I access the existing variable of JavaScript in C#?

The answer is as follows:

Method 1:1. The foreground USES static text control to hide the field and writes the value of js variable into it. 2. The background USES request["id"] to get the value;

Method 2: You can use cookie or session

Question 4. How do I access the JavaScript function in C#?

The answer is as follows:

c# code to execute the javaScript function:

Method 1:1,

Page.RegisterStartupScript("ggg"," "); 

Method 2: Use the Literal class, then

private void Button2_Click(object sender, System.EventArgs e) 
string str; 
str=" "; 

Related articles: