C # code and javasMutual call of functions:
1. HowIngress accesses C # functions?
2. HowIngress accesses C # VARIABLES?
3. How to access javas in C #Is there an existing variable for the consumer?
4. How to access javas in C #Functions?
The answer to question 1 is as follows:
JavasExecute the functions in C # code in the functions function:
Method 1: Combine pages and page classes
1. function declaration is public
Background code (change public to protected)
Public String SS ()
{
Return ("");
}
2. You can use <% = SS () %> in HTML to call // The function name in the C # background.
Foreground script
<Script language = javasLatency>
VaR A = "<% = SS () %>"; // calls the C # background function in JavaScript.
Alert ();
</SCRIPT>
Method 2: javascript asynchronous calls are defined on the ASP. NET page.
1. Declare this method as public );
2. Declare this method as a class method (static in C #, shared in VB. NET), instead of an instance method;
3. Add the webmethod attribute to this method.
4. Set the enablepagemethods attribute of the scriptmanager control on the page to true;
5. Use the following JavaScript syntax on the client to call the PAGE method:
Pagemethods. [methodname] (param1, param2,..., callbackfunction );
6. Specify a callback function for the client to call it asynchronously, and accept and process the returned value in the callback function;
7. Add using system. Web. Services;
Example:
Front-end JavaScript code
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<SCRIPT type = "text/JavaScript">
Function jscallcsharp (param1)
{
Pagemethods. sayhell (param1, onsayhellosucceeded); // sayhell is the parameter of the method param1 that is marked with the [webmethod] attribute in the background. onsayhellosucceeded is the callback function mainly processing the results returned by the background
}
Function onsayhellosucceeded (result) // bound callback function
{
Alert (result );
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: scriptmanager id = "scriptmanager1" runat = "server" enablepagemethods = "true"> // The scriptmanager control manages scripts,
Note: Set enablepagemethods = "true ".
</ASP: scriptmanager>
<Div>
<Input type = "button" onclick = "jscallcsharp ('hello')"/>
</Div>
</Form>
</Body>
</Html>
Background code (. CS file)
Using system;
Using system. configuration;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Using system. Web. Services; // Add web service reference
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
[Webmethod] // indicates the Web service method attribute
Public static string sayhell (String say) // note the modifier of the function, which can only be static
{
Return say;
}
}
Method 3: javascript asynchronous calls are defined in the Web service class.
1. Add a web service to mark this service as allowing ASP. Net ajax to call this web service from the script
The corresponding attribute is [system. Web. Script. Services. scriptservice].
2. Declare the public method and mark it as the [webmethod] attribute method.
3. On the page, add the scriptmanager control and Web Service reference <services> <asp: servicereference Path = "~ /WebService. asmx "/> </services>
4. Use the following JavaScript syntax on the client to call the Web service method:
WebService. helloworld ("helloword", function (RES) // WebService is a Web Service Page name. helloword is a method in the Web Service Page class. function is a callback JavaScript function, mainly used to process the returned results.
{
Alert (RES );
});
Example:
Page code
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<SCRIPT type = "text/JavaScript">
Function jscallcsharp (param1)
{
Pagemethods. sayhell (param1, onsayhellosucceeded );
}
Function onsayhellosucceeded (result)
{
Alert (result );
}
// This method is a called function.
Function jscallwebservice ()
{
WebService. helloworld ("helloword", function (RES) // call the Web Service
{
Alert (RES );
});
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: scriptmanager id = "scriptmanager1" runat = "server" enablepagemethods = "true">
<Services> <asp: servicereference Path = "~ /WebService. asmx "/> </services> // You must reference the web service.
</ASP: scriptmanager>
<Div>
<Input type = "button" onclick = "jscallcsharp ('hello')" value = "Test C # background page"/>
<Input type = "button" onclick = "jscallwebservice ()" value = "test Web Background class"/>
</Div>
</Form>
</Body>
</Html>
Web Service background code
Using system;
Using system. collections;
Using system. LINQ;
Using system. Web;
Using system. Web. Services;
Using system. Web. Services. Protocols;
Using system. xml. LINQ;
/// <Summary>
/// Summary of WebService
/// </Summary>
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream.
[System. Web. Script. Services. scriptservice] // you must add this identifier.
Public class WebService: system. Web. Services. WebService {
Public WebService (){
// If you use the designed component, uncomment the following line
// Initializecomponent ();
}
[Webmethod] // attributes to be marked by the method
Public String helloworld (string result ){
Return result;
}
}
Question 2:Ingress accesses C # VARIABLES
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 = 'javasComment '> var temp = "+ TMP +" </SCRIPT>"
TMP is the background variable, and then you can directly access temp in JS to obtain the value.
Question 3. How to access javas in C #Is there an existing variable for the consumer?
Method 1: 1. the foreground uses the server text control to hide the field and write the JS variable value into it. The background directly accesses and calls the field through the control ID.
Method 2: use cookies or sessions to store variable values.
Question 4 C # code execution of JavaScript Functions and calls of JavaScript Functions
Method 1:. Use response. Write (<SCRIPT> </SCRIPT>) as few as possible. This method will affect CSS and cause page effect deviation.
Method 2: Use scriptmanager in C. registerstartupscript (this, this. getType (), "edit", "csharpcalljs ('" + param1 + "', '" + param2 + "')",
Example:
Script Functions
Function csharpcalljs (param1, param2)
{
Alert (param1 + param2 );
}
Page background code
Scriptmanager. registerstartupscript (this, this. getType (), "edit", "csharpcalljs ('" + param1 + "', '" + param2 + "');", true ); // code for calling the page script function using key code
Method 3: Use Page in C. registerstartupscript ("ggg", "<SCRIPT> csharpcalljs" "+ param1 +", "+ param2 +"); "</SCRIPT> ");
Method 4: Use the literal class in C # (similar to the label class)
Sample Code
Private void button2_click (Object sender, system. eventargs E)
{
String STR;
STR = "<script language = 'javasComment '> ";
STR + = "selectrange ()";
STR + = "</SCRIPT> ";
// Literal1.visible = true;
Literal1.text = STR;
}
The above is a summary of my work. It mainly summarizes the interaction between JavaScript and the background and the interaction between the Asp.net frontend and the background.