SoFunction
Updated on 2025-03-07

Summary of interaction methods between .net and javascript scripts

The examples in this article summarize the interaction methods between .net and javascript scripts and share them with you for your reference. The specific methods are as follows:

Call js

Copy the codeThe code is as follows:
("<script language=javascript>");
("alert('Welcome to the Peak');" );
("='';") ;
("</script>") ;

In this case, you can call the functions of the JS script in the page.

2. How to access the value of the server control by js script

There is a TextBox control on the interface with the ID Name. You can use the following script to get the Name value in js.

Copy the codeThe code is as follows:
var myvalue=('Name').value;

3. How to get the value of a variable in js
The method is to put a hidden control HtmlInputHidden on the interface and set it to run as a server control, so that the value of the control can be accessed in the js script and in the code.
Assign values ​​to server controls in js:
Copy the codeThe code is as follows:
var bt=('Name').value;
='name';

Used in to access.

4. Calling of functions between the foreground and the background

Copy the codeThe code is as follows:
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function callServer(arg){
var oTb = ('<%= %>');
// A variable passed to the server in arg
arg = ;
<%=(this, "arg", "receiveServerResult", null, true)%>
}
function receiveServerResult(result){
// Add the logic to process the results returned by the server here. The result variable is the result returned by the server.
alert(result);
}
</script>
...// Some codes are omitted here
<asp:TextBox ID="editValue" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit Data" OnClientClick="callServer();return false;" />
</head>

CS file:
C# code

Copy the codeThe code is as follows:
//The page class inherits the ICallbackEventHandler interface and implements two methods
public partial class _Default : , ICallbackEventHandler
{
private string m_strResult = "";
#region ICallbackEventHandler Members
public string GetCallbackResult()
{
// Return the server-side processing result to receiveServerResult method
return m_strResult;
}
public void RaiseCallbackEvent(string eventArgument)
{
// eventArgument is a variable transmitted from the client, corresponding to arg variable
// Add server-side processing logic here...
m_strResult = eventArgument;
}
#endregion
}

Execute functions in C# code in functions:

Method 1:
①. First create a button and write the call or processed content into button_click in the background;
②. Write a js function in the front desk, the content is
Copy the codeThe code is as follows:
("btn1").click();

③. Calling js functions in the foreground or background to inspire click events is equivalent to accessing the background c# function;

Method 2:
①. Function declaration is public
Background code (it is also OK to change public to protected)

Copy the codeThe code is as follows:
public string ss()
{
return("a");
}

②. Use <%=fucntion()%> in html to call
Front desk script
Copy the codeThe code is as follows:
<script language=javascript>
var a = "<%=ss()%>";
alert(a);
</script>

Method 3:
①、

Copy the codeThe code is as follows:
<script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument)
{
var theForm = document.Form1; //Refers to the form of runat=server
theForm.__EVENTTARGET.value = eventTarget;
theFrom.__EVENTARGUMENT.value = eventArgument;
();
}
-->
</script>
<input type="button" name="Button1" value="button" onclick="javascript:doPostBack('Button1','')">

Method 4:
Copy the codeThe code is as follows:
<script language="javascript">
function SubmitKeyClick()
{
if ( == 13)
{
= true;
= false;
="The name of the function you want to call";
[0].submit();
}
}
</script>
<INPUT onkeypress="SubmitKeyClick()" type="text">
<input type="hidden" name="FunName"> <!--To store the function you want to call -->

In .CS there are:
Copy the codeThe code is as follows:
public Page_OnLoad()
{
if (!())
{
string strFunName=["FunName"]!=null?["FunName"]:"";
//Decide which function to call based on the value passed back
switch(strFunName)
{
case "enter()":
enter() ; //Call this function
break;
case "Other":
//Calling other functions
break;
default:
//Calling the default function
break;
}
}
}
public void enter()
{
//…For example, calculate a certain value
}

6. Access C# variables in JavaScript

Method 1: Access through the hidden domain on the page

Copy the codeThe code is as follows:
<input type="hidden" runat="server">

Method 2: If the background defines PUBLIC STRING N; the format of referenced this variable in the foreground js is '<%=n%>' or "+<%=n%>+"
Method 3: Or you can register a script on the page after the server-side variable assignment
"<script language='javascript'>var temp=" + tmp + "</script>"
tmp is a background variable, and then you can directly access temp to get the value in js.

7. Access JavaScript functions in C#

Execute javaScript functions in c# code:
Method 1:

Copy the codeThe code is as follows:
("ggg","<script>SetVisible(1); </script>");

Method 2: Use the Literal class, and then
Copy the codeThe code is as follows:
private void Button2_Click(object sender, e)
{
string str;
str="<script language='javascript'>";
str+="selectRange()";
str+="</script>";
//=true;
=str;
}

I hope this article will be helpful to everyone's C# programming.