如何根据自动完成扩展程序返回的值从数据库进行搜索 [英] how to make a search from database based on values returned by Autocomplete extender

查看:61
本文介绍了如何根据自动完成扩展程序返回的值从数据库进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我''使用ASP.net2.0和SQL server2005.我已经安装了AJAX工具包.

我想进行类似于Google搜索的搜索,即从数据库中检索基于用户键入字符并单击搜索的列表.

我使用了一个文本框和AJAX自动完成扩展器.我已与DB建立连接,并能够从字段中创建列表.但是当我选择一个列表元素并单击搜索按钮时,单击事件不会进入搜索按钮.无需使用自动完成功能,我就可以检索记录并将其显示在数据网格视图中.我是否需要转换列表返回到数组.请帮我

我的aspx页面是

I'' using ASP.net2.0 and SQL server2005.I have installed AJAX toolkit.

i want to make a search similar to google search ie retrieve from a database a list based on user typing characters and clicking search.

i have used a text box and AJAX autocomplete extender .i have made a connection to DB and able to make a list from a field. But when i select one list elment and click search button it doesn''t go into the Search button on click event.Without using autocomplete i''m able retrieve records and display it on data grid view.Do i need to convert the list returned to an array.Plz help me

My aspx page is

<%@ 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"  EnablePageMethods="true" runat="server" />
        <div>
            <cc1:AutoCompleteExtender ServiceMethod="SearchCD" MinimumPrefixLength="2"

    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"

    TargetControlID="txtSearch" ID="AutoCompleteExtender1" runat="server" FirstRowSelected="true">
            </cc1:AutoCompleteExtender>
            &nbsp;
            <asp:TextBox ID="txtSearch" runat="server" AutoPostBack="True"></asp:TextBox>
            <asp:GridView ID="dgv1" runat="server">
            </asp:GridView>
            <asp:Button ID="btnSearch" runat="server" Text="Search" /></div>
    </form>
</body>
</html

>



我的.aspx.cs是



My .aspx.cs is

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> SearchCD(string prefixText, int count)
    {
        

        
        string constring = ConfigurationManager.AppSettings.Get("con").ToString();
        SqlConnection conn = new SqlConnection(constring);


        SqlCommand cmd = new SqlCommand("select CD_NAME from CD_DETAILS where " + "CD_NAME like @SearchText + ''%''", conn);


        cmd.Parameters.AddWithValue("@SearchText", prefixText);
        cmd.Connection = conn;
        conn.Open();
        List<string> cds = new List<string>();
        using (SqlDataReader sdr = cmd.ExecuteReader())
        {
            while (sdr.Read())
            {
                cds.Add(sdr["CD_NAME"].ToString());
            }
        }
        conn.Close();
        
        return cds;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {

        string constring = ConfigurationManager.AppSettings.Get("con").ToString();
        SqlConnection conn = new SqlConnection(constring);
        conn.Open();
        SqlCommand check = new SqlCommand("spSearch", conn);
        check.CommandType = CommandType.StoredProcedure;
        check.Parameters.Add(new SqlParameter("@searchKey", SqlDbType.NVarChar, 50));

        check.Parameters["@searchKey"].Value = txtSearch.Text;

        SqlDataAdapter da = new SqlDataAdapter(check);
        DataSet ds = new DataSet();
        da.Fill(ds);
        this.dgv1.DataSource = ds.Tables[0].DefaultView;

        dgv1.DataBind();

        conn.Close();

    }
   
}

推荐答案

click事件未连接

The click event is not hooked up

<asp:button id="btnSearch" runat="server" text="Search" xmlns:asp="#unknown" />


应该是


should be

<asp:button id="btnSearch" runat="server" text="Search" onclick="btnSearch_Click" xmlns:asp="#unknown" />


这篇关于如何根据自动完成扩展程序返回的值从数据库进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆