Ajaxcontroltoolkit is a useful package for us to work with ASP. net Ajax. there some extenders and controls need WebService to return the data. for example, autocompleteextender, cascadingdropdown, numericupdownextries and so on. for more details about ajaxcontroltoolkit, please refer to this link: http://www.asp.net/ajaxlibrary/act.ashxHere we talked about the autocompleteextender as an example.
There are two ways to create a WebService method for the extender, in an isolated Web Service Page (*. asmx), in the code-behind of the page which use the extender.
A) create an isolated Web Service Page
Here is a full web service page for an autocompleteextender.
Some tips we need to pay attention:
- First, we need [system. Web. Script. Services. scriptservice] for the service class.
- Second, we need [webmethod] For the method.
- Third, all the parameters names cocould not be changed.
- Forth, in a web service page, weDon't needThe"Static"Qualifier.
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
[System. Web. Script. Services. scriptservice]
Public Class AutoComplete : WebService
{
Public AutoComplete ()
{
}
[webmethod]
Public string [] getcompletionlist ( string prefixtext , int count )
{
If ( count = 0 )
{
count = 10 ;
}
If(Prefixtext.Equals("XYZ"))
{
Return New String[0];
}
Random Random = New Random ();
List < String > Items = New List < String > ( Count );
For ( Int I = 0 ;I < Count ; I ++)
{
Char C1 = ( Char ) Random . Next ( 65 , 90 );
Char C2 = ( Char ) Random . Next ( 97 , 122 );
Char C3 = (Char ) Random . Next ( 97 , 122 );
Items.Add(Prefixtext + C1 + C2 + C3);
}
Return Items.Toarray();
}
}
B) service method in the page code behind
Here is a code snippet for the service method in the page.
Some tips need to pay attention here.
- First, we need [system. Web. Services. webmethod] for the service method.
- Second, we need [system. Web. Script. Services. scriptmethod] for the service method.
- Third, all the parameters names cocould not be changed.
- Forth, in an inline web service method, weNeedThe"Static"Qualifier.
Public Partial Class Getcustomer : System . Web . UI . Page
{
Protected Void Page_load ( Object Sender , Eventargs E )
{
}
[System. Web. Services. webmethod]
[System. Web. Script. Services. scriptmethod]
Public Static String [] Getcompletionlist ( String Prefixtext , Int Count )
{
If ( Count = 0 )
{
Count = 10 ;
}
If(Prefixtext.Equals("XYZ"))
{
Return New String[0];
}
Random Random = New Random ();
List < String > Items = New List < String > ( Count );
For ( Int I = 0 ;I < Count ; I ++)
{
Char C1 = ( Char ) Random . Next ( 65 , 90 );
Char C2 = ( Char ) Random . Next ( 97 , 122 );
Char C3 = (Char ) Random . Next ( 97 , 122 );
Items.Add(Prefixtext + C1 + C2 + C3);
}
return items . toarray ();
}
}