Sunday, 5 May 2013

How to use autocompleteextender Ajax Control example with textbox in asp.net


How to use autocompleteextender  Ajax Control example with textbox in asp.net

// For  Sql Server 2008 ……………………………..

create database test
use test
create table StateName(SrNo int identity(1,1),StateCode nvarchar(50) primary key,StateName nvarchar(50) unique)
insert into StateName values('Mahar11','Maharashtra')
insert into StateName values('Madhy11','Madhya Pradesh')
insert into StateName values('Manip11','Manipur')
insert into StateName values('Megha11','Meghalaya')
select * from StateName


// Create  connectionstring in  web.config  file……………………………

<connectionStrings>
<add name="kush" connectionString="Data Source=KUSH-PC\KUSH;Initial Catalog=test;Integrated Security=True"/>
</connectionStrings>


// How  to  apply   AutoCompleteExtender  on textbox.........................



<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
<div>
<table width="500px" border="5px">
<tr><td colspan="2">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </asp:ToolkitScriptManager></td>
 </tr>
    <tr><td>
        <asp:Label ID="Label1" runat="server" Text="StateName"></asp:Label>
        </td><td>
        <asp:TextBox ID="txtstatename" runat="server"></asp:TextBox>
    
 // after add AutoCompleteExtender write  this code …………………..
      
        <asp:AutoCompleteExtender ID="txtstatename_AutoCompleteExtender" runat="server" MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="1000" ServiceMethod="GetStateNames" TargetControlID="txtstatename">
        </asp:AutoCompleteExtender>
    
  </td></tr>
  </table>
   </div>
 </form>
</body>
</html>




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class AutoExtenderExample : 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> GetStateNames(string prefixText)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["kush"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select * from StateName where StateName like @State+'%'", con);
cmd.Parameters.AddWithValue("@State", prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
List<string> StateNames = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
 StateNames.Add(dt.Rows[i][2].ToString());
}
return StateNames;
}
}


0 comments:

Post a Comment