loading ...
搜狐圈子 IT数码 web开发 浏览帖子

来自圈子:web开发 (41 人)

圈子描述:创造是一种乐趣
圈子标签:web 开发
web开发
副圈主:
共1页 | 上一页   1   下一页

AutoCompleteExtender 自动输入完成 (原创) 2/?

标签:

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();
    }
}

效果图:

1

总觉得ms的那套东东不怎么好用.所以.压根就没有用过.autocomplete都是js完成的. 谄媚
我是美丽签名档学会轉身前,伱沒有看到我的淚水;只有我以往的微笑祝福你;轉身后,我只有自己去品嘗淚水的味道......
引用 | 回复 | 发表时间:2008-03-20

2

我用了 怎么没反应
引用 | 回复 | 发表时间:2008-04-28
搜狐网友
共1页 | 上一页   1   下一页