2016年6月1日 星期三

AutoCompleteExtender By Ajax Using WebService

CSS
1
2
3
4
.CompletionListCssClass
{
    z-index:99999 !important;
}


aspx
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<asp:TextBox ID="TextBox_PY" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender 
   ID="AutoCompleteExtender1"
   runat="server"
   MinimumPrefixLength="1"
   TargetControlID="TextBox_PY"
   ServiceMethod="GetCompletionList_PY"
   ServicePath="WebService/WebServiceTest.asmx"
   CompletionSetCount="20"
   CompletionListCssClass ="CompletionListCssClass"
/>


WebService.asmx.cs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace Project.WebService
{
    /// <summary>
    ///WebServiceTest 的摘要描述
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下列一行。
    [System.Web.Script.Services.ScriptService]
    public class WebServiceTest : System.Web.Services.WebService
    {
        [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        //方法不可宣告static
        public string[] GetCompletionList_PY(string prefixText, int count)
        {
            //資料庫連線字串
            string connStr = @"Data Source=.;Initial Catalog=DatabaseName;User ID=id;Password=pw;";

            ArrayList array = new ArrayList();//儲存撈出來的字串集合

            using (SqlConnection conn = new SqlConnection(connStr))
            {
                DataSet ds = new DataSet();
                string selectStr = @"SELECT Top (" + count + ") [PY] FROM [View_PYList] Where [PY] Like '" + prefixText + "%' Order by [PY] ASC";
                SqlDataAdapter da = new SqlDataAdapter(selectStr, conn);
                conn.Open();
                da.Fill(ds);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    array.Add(dr["PY"].ToString());
                }
            }
            return (string[])array.ToArray(typeof(string));
        }
    }
}


AutoCompleteExtender參考:https://dotblogs.com.tw/shadow/archive/2011/05/08/24535.aspx

CSS參考:http://note.tc.edu.tw/598.html

沒有留言 :

張貼留言