zoukankan      html  css  js  c++  java
  • [ASP.NET AJAX]Important notes when we create web service methods for some extenders in AjaxControlToolkit package

    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, NumericUpDownExtender and so on.
    For more details about AjaxControlToolkit, please refer to this link:
    http://www.asp.net/ajaxlibrary/act.ashx

    Here 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 could not be changed.
    • Forth, in a web service page, we Don't Need the "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 could not be changed.
    • Forth, in an inline web service method, we NEED the "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();
    }
    }
  • 相关阅读:
    SDN网络笔记【毕设-SDN网络】
    Latex笔记【Latex】
    小米路由器 3G 开启SSH 安装 MT 工具箱 【环境搭建,小米路由器】
    windows 下安装linux子系统及其可视化【Linux】
    11月1日数据结构讨论班 【杂】
    简单远程遥控程序【网络程序设计
    VPS使用小结【VPS】
    vim使用总结【Vim】
    域名解析【网络程序设计
    MySQL数据库修改字段名、字段类型、字段长度
  • 原文地址:https://www.cnblogs.com/JerryWeng/p/1854351.html
Copyright © 2011-2022 走看看