Methods for js to call functions in Cs and methods for calling JS functions in CS [summary 〕

Source: Internet
Author: User

How to call functions in CS using JS

Write <% method (); %> In the front-end JS Code

Example:

In the CS file

Public void method ()

{

... Execute some operations.

}

This function is then called in the JS on the front-end page.

<SCRIPT type = "text/JavaScript">

<% Method (); %>

</SCRIPT>

 

Call JS functions in CS

Method 1: clientscript. registerstartupscript (GetType (), "event or attribute name", "event or attribute content ");
Example:

Protected void page_load (Object sender, eventargs E)
{

String TTT = "TTT ";

String JS = "<SCRIPT type = 'text/JavaScript '> ";
JS + = "function istwo (m) {alert (m) ;}</SCRIPT> ";

Clientscript. registerstartupscript (GetType (), "registerjs", JS );

Clientscript. registerstartupscript (GetType (), "runjs", "istwo (" + TTT + ")");
}

Method 2: Control ID. Attributes. Add ("event or attribute name", "event or attribute content ");

Example:

Btnpost. Attributes. Add ("onclick", "Return checkinput ()");

 

Method 3: directly output JS Code as a string to the client for interpretation and execution by the client.

Example:

Response. Write ("<SCRIPT> function fun () {alert (" + M + ")} </SCRIPT> ")

 

 

 

========================================================== ====================

Switch: frontend JavaScript and background C # functions in. Net call each other
Article Source: DIY tribe (http://www.diybl.com/course/1_web/javascript/jsjs/200798/71020.html)

C # mutual calls between code and JavaScript Functions

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;

Example:

Code

 <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> List page </title>
<Style type = "text/CSS">
. Display
{
Display: none;
}
</Style>
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function download (SI)
{
Document. getelementbyid ("BTN"). Click ();
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: button id = "BTN" runat = "server" onclick = "btn_click" text = "" cssclass = "display" "/> // note: in this example, the button control cannot be hidden by adding visible = "false ".
</Form>
</Body>
</Html>

 

 

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" 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 = "Name of the function you want to call ";
Document. Form [0]. Submit ();
}
}
</SCRIPT>

<Input onkeypress = "submitkeyclick ()" 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

4. How to access JavaScript Functions in C?
The answer is as follows:
C # execute JavaScript Functions in the Code:
Method 1: 1. Page. registerstartupscript ("ggg", "<SCRIPT> setvisible (1); </SCRIPT> ");
Method 2: Use the literal class and
Private void button2_click (Object sender, system. eventargs E)
{
String STR;
STR = "<script language = 'javascript '> ";
STR + = "selectrange ()";
STR + = "</SCRIPT> ";
// Literal1.visible = true;
Literal1.text = STR;
}

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.