Javascript directly calls the server C # code asp.net Ajax Instance _ practical Tips

Source: Internet
Author: User

In Ms Ajax, JS and C # interaction is a way to invoke WebService, the webservice can be either ASMX or WCF, regardless of the way, the system will automatically generate agent for Developers JS class. The implementation method is as follows:

1. Set up a Web site and add a WCF service here (be sure to select ajax-enabled WCF services) as shown in the following illustration:

2. The IDE automatically generates an SVC file for us, an external interface, and the corresponding backend implementation class for the SVC, which is placed under App_Code, as shown in the following illustration:

3. Modify the code for the class as follows:

Copy Code code as follows:

[ServiceContract (Namespace = "Testajax")]
[Aspnetcompatibilityrequirements (Requirementsmode = aspnetcompatibilityrequirementsmode.allowed)]
public class Service
{
[OperationContract]
public bool ValidateUser (string uid, string pwd)
{
if (uid== "sa" &&pwd== "sa")
{
return true;
}
return false;
}
}

4. Now we can call the page, first in the page to add a ScriptManager, and introduce the WCF we have just written WebService (the purpose is to generate JS at runtime of the proxy class), as follows:
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:scriptmanager id= "ScriptManager1" runat= "Server" >
<Services>
<asp:servicereference path= "~/service.svc"/>
</Services>
</asp:ScriptManager>
</div>
</form>
</body>

5. Next, you can write JS code to directly call C # written webservice. The JS code looks like this:
Copy Code code as follows:

<script type= "Text/javascript" >
Function ValidateUser (UID, pwd) {
TestAjax.Service.ValidateUser (Uid,pwd,onsucceed, onfailed);
}
function Onsucceed (Result) {
if (result = = True) {
Window.alert ("Through verification");
}
else {
Window.alert ("Validation failed!") ");
}
}
function onfailed (Result) {
Window.alert ("Operation failed:" +result. _message);
}
</script>

6. Please note here that When the TestAjax.Service.ValidateUser method is invoked, the return value of the function is not directly in the code, because the invocation of the server function using this scenario is asynchronous, and the correct approach is to specify two callback functions Onsucceed and onfailed , the first function is a successful callback, the latter is a failure of the callback, both functions require a parameter, the Onsucceed parameter is the return value of the server function, and onfailed parameter is a failure of the error message, features a bit like exception type, which _ Message property, the stack trace information for an error in the _stacktrace.
7. Don't say this callback method trouble! In fact, this is a regular asynchronous callback pattern, which is written in most cases (no matter what language)!
8. The complete code on the page looks like this:
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<script type= "Text/javascript" >
Function ValidateUser (UID, pwd) {
TestAjax.Service.ValidateUser (Uid,pwd,onsucceed, onfailed);
}
function Onsucceed (Result) {
if (result = = True) {
Window.alert ("Through verification");
}
else {
Window.alert ("Validation failed!") ");
}
}
function onfailed (Result) {
Window.alert ("Operation failed:" +result. _message);
}
function Button1_onclick () {
var uid = $get ("Tbxuid"). Value;
var pwd = $get ("Tbxpwd"). Value;
ValidateUser (UID,PWD);
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:scriptmanager id= "ScriptManager1" runat= "Server" >
<Services>
<asp:servicereference path= "~/service.svc"/>
</Services>
</asp:ScriptManager>
</div>
User name: <input id= "Tbxuid" type= "text"/><br/>
Password: <input id= "Tbxpwd" type= "text"/>
<input id= "Button1" type= "button" value= "verify" onclick= "Return Button1_onclick ()"/>
</form>
</body>

9. The results of the operation are shown below:
Validation occurs when both the username and password are SA
Validation is not possible when username and Ganyou have a value that is not an SA


10. Do you have any questions for me to email it: warensoft@foxmail.com

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.