ASP.net 2.0 Ajax WebService Call Method Example

Source: Internet
Author: User
Tags format eval query requires serialization trim web services
Ajax|asp.net|web| sample

ASP.net 2.0 ajax can easily call server WebService in Client JS, here are some examples of calls. The author installs the ASP.net 2.0 AJAX

The version is Ajax November CTP.

The three examples were:
1 ws method with parameters
2 ws method with no parameters
3 ws method with parameter type as DataTable

First, WebMethod
Note The main points:
1 WebMethod class needs to add namespace Microsoft.Web.Script.Services, this space needs to reference Microsoft.Web.Preview.dll
2 class declarations Add tags [scriptservice]
3 in asp.net 2.0 you can use a DataTable directly as the return type, but you need to add the properties of the serialization converter to the Web.config file. Datasets, DataTable, DataRow all have converters

<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization>
<converters>
<add name= "Datasetconverter" type= "Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview "/>
<add name= "Datarowconverter" type= "Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview "/>
<add name= "Datatableconverter" type= "Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter , Microsoft.Web.Preview "/>
</converters>
</jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
Web Services 1:WS1

Using System;
Using System.Web;
Using System.Collections;
Using System.Web.Services;
Using System.Web.Services.Protocols;
Using Microsoft.Web.Script.Services;
Using System.Data;
/**////<summary>
Summary description of WS1
</summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
[ScriptService]
public class WS1:System.Web.Services.WebService {

Public WS1 () {

If you are using a design component, uncomment it to follow the line
InitializeComponent ();
}

[WebMethod]
public string Servertime ()
{
Return String.Format (' Now: {0} ', DateTime.Now);
}

[WebMethod]
Public DataTable getdatatable ()
{
DataTable dt = new DataTable ("Person");

Dt. Columns.Add (New DataColumn ("Name", typeof (String));
Dt. Columns.Add (New DataColumn ("LastName", typeof (String));
Dt. Columns.Add (New DataColumn ("Email", typeof (String));

Dt. Rows.Add ("Kui", "he", "hekui168@163.com");
Dt. Rows.Add ("Ren", "Chao", "chaoren888@163.com");

return DT;
}
}


Web Services 2:ws

Using System;
Using System.Web;
Using System.Collections;
Using System.Web.Services;
Using System.Web.Services.Protocols;
Using Microsoft.Web.Script.Services;

/**////<summary>
Summary description of WS
</summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
[ScriptService]
public class WS:System.Web.Services.WebService {

Public WS () {

If you are using a design component, uncomment it to follow the line
InitializeComponent ();
}

[WebMethod]
[Scriptmethod (Usehttpget = True)]
public string HelloWorld (string query)
{
String inputstring = Server.HTMLEncode (query);
if (! String.IsNullOrEmpty (inputstring))
{
return String.Format ("Hello, {0}.", inputstring);
}
Else
{
Return ' query string is null or empty ';
}
}

}


Second, the front page:
Note The main points:
The methods you need to use for the background webservice are set in the following locations

<asp:scriptmanager id= "ScriptManager1" runat= "Server" >
<Services>
<asp:servicereference path= "~/ws.asmx"/>
<asp:servicereference path= "~/ws1.asmx"/>
</Services>
</asp:ScriptManager>
Default page:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML 1.1//en" "Http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >
<title>untitled page</title>

<script language= "javascript" type= "Text/javascript" src= "Js.js" >
</script>

<body>
<form id= "Form1" runat= "Server" >
<asp:scriptmanager id= "ScriptManager1" runat= "Server" >
<Services>
<asp:servicereference path= "~/ws.asmx"/>
<asp:servicereference path= "~/ws1.asmx"/>
</Services>
</asp:ScriptManager>
<div>
<asp:button id= "Button1" runat= "Server" text= "button" onclientclick= "DD (); return false;"/>
<div id= "Time" >
</div>
<div id= "List1" >
<asp:dropdownlist id= "DDL1" runat= "Server" width= "187px" >
</asp:DropDownList>
</div>

</div>
</form>
</body>

Third, JavaScript program:
Note The main points:
The Ajax November CTP requires the Eval () method to convert it into a DataTable object (and to trim the front "("), while the Ajax December CTP supports the following method conversions Sys.Preview.Data.DataTable.parseFromJson (Result) "

function DD ()
{
Ws. HelloWorld (
' Hekui ',
function (Result)
{
alert (result);
}
);
WS1. Servertime (
function (Result)
{
alert (result);
var divtime = document.getElementById ("Time");
divtime.innerhtml = result;
}
);
WS1. Getdatatable (
function (Result)
{
Get to Drop-down Box control
var List = document.getElementById ("DDL1");

AJAX November CTP requires the Eval () method to convert it to a DataTable object (and to trim the front "(")
var text= result.dataArray.substring (0,result.dataarray.length-1);
var Table = eval (Text);

AJAX December CTP supports the following method conversions
var Table = Sys.Preview.Data.DataTable.parseFromJson (result);

Clear dropdown Box Original list item
for (x=list.options.length-1 x >-1; x--)
{
List.remove (0);
}

Add data from the retrieved DataTable to the Drop-down list item
for (x=0 x < Table.length + x + +)
{
Get each row
var Row = table[x];
Create a list item
var option = document.createelement ("option");
List item Display text assignment
Option.text = Row.name + "" + row.lastname;
List Item Option value Assignment
Option.value = Row.email;

To determine the browser type, add items
if (Window.navigator.appName.toLowerCase (). IndexOf ("Microsoft") >-1)
List.add (option);
Else
List.add (option, NULL);
}
}
);
}



Related Article

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.