zoukankan      html  css  js  c++  java
  • 使用AJAX1.0實現自動完成功能

    使用AJAX1.0實現自動完成功能
    下午 04:56 2011/2/14

    一、準備web service
    1、添加web service 文件
    右擊“網站”-->添加新項目-->選擇Web 服務。web service 的 code 文件放在App_Code中,命名為GetPNService.cs。
    2、加入namespace。 using System.Web.Script.Services;
    3、加入標記。[ScriptService]
    4、編寫方法。給方法添加標記——[WebMethod]。方法修飾為public。
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class GetPNService : System.Web.Services.WebService
    {
        public GetPNService()
        {
            //如果使用設計的元件,請取消註解下行程式碼
            //InitializeComponent();
        }
        [WebMethod]
        public string[] GetPNList(string prefixText, int count, string contextKey)
        {
            DataTable dt = null;
            if (count == 0)
            {
                count = 20;
            }
            List<string> pnList = new List<string>(count);
            dt = ERPMateria.GetPNList(contextKey.ToUpper(), prefixText.ToUpper(), count);
            if (dt != null)
            {
                foreach (DataRow row in dt.Rows)
                {
                    pnList.Add(row["pn"].ToString());
                }
            }
            return pnList.ToArray();
        }
    }

    二、添加AutoCompleteExtender,屬性設置如下。
            <ajaxToolkit:AutoCompleteExtender
            ID="AutoCompleteExtender4"
            runat="server"
            TargetControlID="txtInfoMaterialNo"
            ServiceMethod="GetPNList"
         ServicePath="GetPNService.asmx"
            UseContextKey="true"
            MinimumPrefixLength="2"
            EnableCaching="false"
            CompletionSetCount="20"
            CompletionInterval="1000"
            BehaviorID="autoCompleteBehavior4" />
      
    三、編寫UseContextKey
    因為上面UseContextKey="true"。
    protected void Page_Load()
    {
    this.AutoCompleteExtender4.ContextKey = this.ddlSearchPlant.SelectedItem.Text;
    }

    四、注意
    1、編寫webservices時,加入 System.Web.Script.Services 和 [ScriptService] ,否則前端調用不了webservices的方法。
    2、基礎不扎實,原理不清楚,浪費腦細胞和時間。

  • 相关阅读:
    ZOJ 1002 Fire Net
    Uva 12889 One-Two-Three
    URAL 1881 Long problem statement
    URAL 1880 Psych Up's Eigenvalues
    URAL 1877 Bicycle Codes
    URAL 1876 Centipede's Morning
    URAL 1873. GOV Chronicles
    Uva 839 Not so Mobile
    Uva 679 Dropping Balls
    An ac a day,keep wa away
  • 原文地址:https://www.cnblogs.com/htht66/p/1954516.html
Copyright © 2011-2022 走看看