• Home  >  Article  >  Web Front-end  >  JavaScript and asp.net (c#) call each other's methods_javascript skills

    JavaScript and asp.net (c#) call each other's methods_javascript skills

    WBOY
    WBOYOriginal
    2016-05-16 18:39:271604browse

    1. Obtain the aspx client request address:

    Copy code The code is as follows:



    2. C# calls the client method: (top.window.document: table parent form, if the MainFrame object is in this form, do not add top.window. document)
    Method in Js:
    Copy code The code is as follows:

    function navurl (url) {
    $("#mainFrame",top.window.document).attr("src", url);
    }


    C# call:
    Copy code The code is as follows:

    string navUrl = "javascript:navurl('" "http: //" Request.Url.Host ResolveUrl("../Document/callIndex.aspx?FolderId=");
    DocName.NavigateUrl = navUrl txtDocId.Value.Trim() "')";


    3. C# calls client method:
    Copy code The code is as follows:

    page.ClientScript.RegisterStartupScript(page.GetType(), "message", "") ;

    The following problems to be solved are as follows:
    1. How to access C# functions in JavaScript?
    2. How to access C# variables in JavaScript?
    3. How to access existing variables of JavaScript in C#?
    4. How to access JavaScript functions in C#?
    The answer to question 1 is as follows:
    Execute the function in the C# code in the javaScript function:
    Method 1:
    1. First create a button, Write the called or processed content into button_click in the background;
    2. Write a js function in the foreground with the content document.getElementById("btn1").click();
    3. Call it in the foreground or background js function, which triggers a click event, is equivalent to accessing the backend c# function;
    Method two:
    1. Declare the function as public
    Backend code (you can also change public to protected)
    public string ss()
     {
     return("a");
     }
     2. You can call the front-end script in html

     Method 3:

    "SubmitKeyClick()" id="aaa" type="text">
      〈! --Used to store the function you want to call--〉
     In .CS there are:
    public Page_OnLoad()
     {
     if (!Page.IsPost())
      {

       {
        case "enter()":
       enter() ; //Call this function
       break; break;
       default:
         //Call the default function
       break; ...For example, calculating a certain value
     }

    Question 2. How to access C# variables in JavaScript?
    The answer is as follows:
    Method 1: 1. Access
     
    Method 2: 1. If PUBLIC STRING N is defined in the background; the format for referencing this variable in the front-end js is '' or " "
     
    Method three: 1. Or you can register a script on the page after assigning values ​​to server-side variables
    ""
    tmp is a background variable, and then you can directly access temp in js to get the value.

    Question 3. How to access existing variables of JavaScript in C#?
    The answer is as follows:
    Method 1: 1. Use a static text control to hide the field in the front desk and write the js variable value into it; 2. Use request["id"] in the background to obtain the value;
    Method 2: You can use cookie or session

    Question 4. How to access JavaScript functions in C#?
    The answer is as follows:
    Execute javaScript function in c# code:
    Method one: 1.
    Page.RegisterStartupScript("ggg"," ");
    Method two: Use Literal class, and then
    private void Button2_Click(object sender, System.EventArgs e)
     {
     string str;
     str=" ";
      //Literal1.Visible=true;
    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn