Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

XML
<%@ 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
>


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

    }
   
}
Posted
Updated 5-Sep-10 9:55am
v2

1 solution

The click event is not hooked up

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

should be
C#
<asp:button id="btnSearch" runat="server" text="Search" onclick="btnSearch_Click" xmlns:asp="#unknown" />
 
Share this answer
 
v2
Comments
shanboy 6-Sep-10 12:52pm    
hi mark
Thanx for ur help
Sorry to make u go through full code for a silly problem
Thanx a lot
bye..
shanboy 6-Sep-10 12:53pm    
sorry..silly mistake

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900