C # simple interaction with JavaScript

Source: Internet
Author: User

1.asp.net call JS

Response. Write ("<script language = JavaScript> ");
Response. Write ("alert ('Welcome to the Summit ');");
Response. Write ("location. href = 'login. aspx ';");
Response. Write ("</SCRIPT> ");

In this case, you can call all JS script functions on the page.

2. How does a JS script access the server control value?

There is a Textbox Control on the interface, whose ID is name. The following script can be used in JS to obtain the name value.
VaR myvalue = Document. All ('name'). value;

3. How to obtain the value of a variable in JS from a server control?

The method is to place a hidden control htmlinputhidden on the interface and set it to run as a server control, so that the control value can be accessed in JS scripts and ASP. NET code.
Assign a value to the server control in JS:
VaR bt = Document. All ('name'). value;
Bt. value = 'name ';
Use name. Value to access ASP. NET.

4. Call functions between the foreground and the background

<Head runat = "server">
<Title> untitled page </title>
<SCRIPT type = "text/JavaScript">
Function callserver (ARG ){
VaR OTB = Document. getelementbyid ('<% = editvalue. clientid %> ');
// Arg is the variable passed to the server
Arg = OTB. value;
<% = Clientscript. getcallbackeventreference (this, "Arg", "eseserverresult", null, true) %>
}

Function exploreserverresult (result ){
// Add the logic for processing the server's returned results here. The result variable is the result returned by the server.
Alert (result );
}
</SCRIPT>
... // Some code is omitted here
<Asp: textbox id = "editvalue" runat = "server"/>
<Asp: button id = "btnsubmit" runat = "server" text = "submit data" onclientclick = "callserver (); Return false;"/>

</Head>

CS files

C # code
// The page class inherits the icallbackeventhandler interface and implements two methods.
Public partial class _ default: system. Web. UI. Page, icallbackeventhandler
{
Private string m_strresult = "";

# Region icallbackeventhandler members

Public String getcallbackresult ()
{
// Return the server processing result to the receiveserverresult Method
Return m_strresult;
}

Public void raisecallbackevent (string eventargument)
{
// Eventargument is a variable sent from the client, corresponding to the ARG variable
// Add the server processing logic here...
M_strresult = eventargument;
}

# Endregion
}

5. 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" 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
}

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

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.

7. How to access JavaScript Functions in C?

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.