利用web 服务实现自动完成输入

利用web 服务实现自动完成输入,废话不多说。前台界面:

 

代码
<asp:textbox id="TextBox1" runat="server"></asp:textbox>
                     
                     
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender3" runat="server" Enabled="true"
             TargetControlID
="TextBox1"
              ServicePath
="/LsComplete.asmx"
              ServiceMethod
="GetCompleteDepart"
              CompletionInterval
="500"
              CompletionSetCount
="10"
              MinimumPrefixLength
="1">
            
</ajaxToolkit:AutoCompleteExtender>

 

后台代码:

新建一个web服务:

 

代码
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// LsComplete 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class LsComplete : System.Web.Services.WebService {

    
public LsComplete () {

        
//如果使用设计的组件,请取消注释以下行 
        
//InitializeComponent(); 
    }

    
private static string[] autoCompleteWordList = null;
    [WebMethod]
    
public String[] GetCompleteDepart(string prefixText, int count)
    {
        
// 如果数组为空
        if (autoCompleteWordList == null)
        {
            
//读取数据库的内容

            
string ConnectionString = ConfigurationSettings.AppSettings["SQLConnectionString"];
            SqlConnection myconn 
= new SqlConnection(ConnectionString);
            myconn.Open();


            
// SqlDataAdapter da = new SqlDataAdapter("select departname from departinfo  where departname like'" + prefixText + "%' order by departname", conn);


            
string mySel = "select title from [表名]  where Rtrim(字段) like'%" + prefixText + "%' ";
            SqlDataAdapter Adpt 
= new SqlDataAdapter(mySel, myconn);

            DataSet ds 
= new DataSet();
            Adpt.Fill(ds);
            
//读取内容文件的数据到临时数组
            string[] temp = new string[ds.Tables[0].Rows.Count];
            
int i = 0;
            
foreach (DataRow dr in ds.Tables[0].Rows)
            {
                temp[i] 
= dr["字段"].ToString().Trim();
                i
++;
            }
            
//将临时数组的内容赋给返回数组
            autoCompleteWordList = temp;
            
if (myconn.State == ConnectionState.Open)
                myconn.Close();
        }
        String[] returnValue 
= new string[count];
        returnValue 
= autoCompleteWordList;
        autoCompleteWordList 
= null;
        
//返回数据
        return returnValue;
    }
    
}

 

 一是备用,二是欢迎高手指点

posted @ 2010-01-11 21:48  FreeComputer  阅读(414)  评论(1编辑  收藏  举报