[ASP. NET Ajax] Important Notes when we create Web service methods for some extenders in ajaxcontrolto

Source: Internet
Author: User
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 ();
}
}

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.