SoFunction
Updated on 2025-03-07

Methods for implementing interaction between C# and JavaScript

In development, the interaction between the backend and the frontend is often used, and here we summarize a little method of interoperating with C# and javascript.

1. Call jacascript method in background c# code

javascript code:

<script type="text/javascript" language="javascript">
  function test()
  {
    alert("oec2003");
    return false;
  } 
</script>

C# code:

protected void Button1_Click(object sender, EventArgs e)
{
  ((), "clear", "<script>test()</script>");
}

Calling c# method

If the method in C# has a return value, you can use the following method
C# code

public string GetAuthStatus()
    {
      ViewState["Auth"] = "Red";
      return ViewState["Auth"].ToString();
    }

javascript code

<script type="text/javascript" language="javascript">
  function getAuth() 
  {
    var authStatus = "<%=GetAuthStatus()%>";
    return authStatus;
  }
</script>

If the c# method called in javascript does not return value, you can put a button on one side, and then write what you want to do in the button click event, and write the following code in the client script.

("button1").click();

The above is the entire content of this article, I hope you like it.