|
AutoCompleteExtender控件可以帮你自动填写TextBox控件(在数据库中查找).属性:TarGetControlID:指定要让"自动输入完成"扩展器要扩展的TextBox控件ID.ServicePath:Web服务的位置路径.ServiceMehod:要调用的Web服务的方法名.方法签名如下:[System.Web.Services.WebMethod][System.Web.Script.Service.ScriptMethod]public string[] GetCompetionList(string prefixText,int count){......}前台代码:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1" MinimumPrefixLength="1" ServiceMethod="GetProductName" ServicePath="WebService.asmx"> </cc1:AutoCompleteExtender> </div> </form></body></html>WebService代码: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.Web.Script.Services;//关键程序集引用using System.Collections.Generic;//关键程序集引用/// <summary>/// WebService 的摘要说明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][ScriptService()]//一定要添加public class WebService : System.Web.Services.WebService { public WebService () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] [ScriptMethod] public string[] GetProductName(string prefixText, int count) { List<string> suggestions=new List<string>();//声明一泛型集合 SqlConnection con = new SqlConnection("server=.;database=NorthWind;uid=sa;pwd=;"); con.Open(); SqlCommand com = new SqlCommand("select distinct productname from Products where productname like @prefixname order by productname", con); com.Parameters.Add("@prefixname",SqlDbType.NVarChar).Value=prefixText + "%"; SqlDataReader sdr = com.ExecuteReader(); while (sdr.Read()) { suggestions.Add(sdr.GetString(0)); } sdr.close(); con.close(); return suggestions.ToArray(); }} |
|