C # Call the frontend and JS methods in the background for intercommunication

Source: Internet
Author: User

I encountered a problem today. I found good information and put it up to record it. In case I forget the address, it's my note!

Many people call client functions on the server, that is, they call the script functions defined in JavaScript scripts in ASP. After research, we found some stubborn methods.

 

 

1. Write the script using the response. Write Method

 

 

For example, after you click the button, you must first operate the database. After the operation is complete, it is displayed that the operation is complete. You can write it in the last place you want to call.
Response. Write ("<SCRIPT type = 'text/JavaScript '> alert (); </SCRIPT> ");

 

 

This method has a defect that the user-defined functions in the script file cannot be called, and only internal functions can be called. The specific user-defined functions can only be called in response. write the function definition in write, such as response. write ("<SCRIPT type = 'text/JavaScript '> function myfun (){...} </SCRIPT> ");

 

 

2. dynamically add scripts using the clientscript class

 

 

The usage is as follows: add code where you want to call a Javascript script function. Make sure that myfun has been defined in the script file.

 

 

Clientscript. registerstartupscript (clientscript. GetType (), "myscript", "<SCRIPT> myfun (); </SCRIPT> ");

 

 

This method is more convenient than response. Write. You can directly call the UDF in the script file.

 

 

3. Add the attributes attribute of a common control.

 

 

For common buttons: button1.attributes. Add ("onclick", "myfun ();");

 

 

It can only be added in onload or in initialization process similar to onload. In addition, the script function is executed first, and the execution sequence cannot be changed.

 

 

Note: In all the above methods, the background Code cannot convert the code of the current page, such as redirect. You need to put the page conversion code in the script.

 

 

Q:

 

 

1. How to access the C # function in JavaScript?

 

 

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

 

 

3. How to access existing JavaScript variables in C?

 

 

4. How to access JavaScript Functions in C?

 

 


 

 

The answer to question 1 is as follows:

 

 

Execute functions in C # code in JavaScript Functions:

 

 

Method 1: 1. Create a button to write the called or processed content to button_click in the background;

 

 


2. Write a JS function on the front-end with the content document. getelementbyid ("btn1"). Click ();

 

 


3. Call JS functions on the frontend or backend to stimulate the click event, which is equivalent to accessing the C # function in the background;

 

 


 

 

Method 2: 1. function declaration is public

 

 


Background code (change public to protected)

 

 


Public String SS ()

 

 


{

 

 


Return ("");

 

 


}

 

 


2. You can call <% = fucntion () %> in HTML.

 

 


Foreground script

 

 


<Script language = JavaScript>

 

 


VaR A = "<% = SS () %> ";

 

 


Alert ();

 

 


</SCRIPT>

 

 

Method 3: 1. <script language = "JavaScript">
<! --

 

 

Function _ dopostback (eventtarget, eventargument)
{
VaR theform = Document. form1;
// Indicates the form of runat = server.

 

 


Theform. _ eventtarget. value = eventtarget;

 

 


Thefrom. _ eventargument. value = eventargument;

 

 


Theform. Submit ();

 

 


}

 

 


-->

 

 


</SCRIPT>

 

 


<Input id = "button1" type = "button" name = "button1" value = "button">

 

 


 

 

Method 4: <script language = "JavaScript">

 

 

Function submitkeyclick ()

 

 

{

 

 


If (event. keycode = 13)

 

 


{

 

 


Event. cancelbubble = true;

 

 


Event. returnvalue = false;

 

 


Document. All. funname. value = "Name of the function you want to call ";

 

 


Document. Form [0]. Submit ();

 

 


}

 

 

}

 

 

</SCRIPT>

 

 


 

 

<Input id = "AAA" type = "text">

 

 

<Input type = "hidden" name = "funname"> <〈! -- Used to store the function you want to call --> --〉

 

 


 

 

In. CS, there are:

 

 

Public page_onload ()

 

 

{

 

 

If (! Page. ispost ())

 

 

{

 

 

String strfunname = request. Form ["funname"]! = NULL? Request. Form ["funname"]: "";

 

 

// Determine which function to call based on the value returned

 

 

Switch (strfunname)

 

 

{

 

 

Case "Enter ()":

 

 

Enter (); // call this function

 

 

Break;

 

 

Case "Others ":

 

 

// Call other functions

 

 

Break;

 

 

Default:

 

 

// Call the default Function

 

 

Break;

 

 

}

 

 

}

 

 

}

 

 


 

 

Public void enter ()

 

 

{

 

 

//...... For example, calculating a value

 

 

}

 

 


 

 

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

 

 

The answer is as follows:

 

 

Method 1: 1. Access <input id = "XX" type = "hidden" runat = "server">

 

 

Method 2: 1. For example, Public String N is defined in the background; the format of the variable referenced in front-end JS is '<% = n %>' or "+ <% = n %> +"

 

 

Method 3: 1. You can register a script on the page after assigning values to variables on the server.

 

 


"<Script language = 'javascript '> var temp =" + TMP + "</SCRIPT>"

 

 


TMP is the background variable, and then you can directly access temp in JS to obtain the value.

 

 


 

 


 

 

3. How to access existing JavaScript variables in C?

 

 


 

 

The answer is as follows:

 

 


 

 

Method 1: 1. the foreground uses static text controls to hide the field and write the JS variable value into it;

 

 


2. the backend uses request ["ID"] to obtain the value;

 

 


 

 

Method 2: cookie or session can be used

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.