Friday, March 25, 2011

AutoCompleteExtender AjaxControl use in asp.net


<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<head runat="server">
    <title>Untitled Page</title>
    <style>
        .AutoExtender
        {
            font-family: Verdana, Helvetica, sans-serif;
            font-size: .8em;
            font-weight: normal;
            border: solid 1px #006699;
            line-height: 20px;
            padding: 10px;
            background-color: White;
            margin-left:10px;
        }
        .AutoExtenderList
        {
            border-bottom: dotted 1px #006699;
            cursor: pointer;
            color: Maroon;
        }
        .AutoExtenderHighlight
        {
            color: White;
            background-color: #006699;
            cursor: pointer;
        }
        #divwidth
        {
          width: 150px !important;   
        }
        #divwidth div
       {
        width: 150px !important;  
       }
 </style>
</head>
<body>
    <form id="form1" runat="server">
    <div ID="divwidth"></div>
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
        <asp:ServiceReference Path="WebService.asmx" />
        </Services>
        </asp:ScriptManager>
        <br />
        <br />
    <asp:TextBox ID="myTextBox" runat="server"          
                 AutoCompleteType="Disabled"></asp:TextBox>
    <cc1:AutoCompleteExtender ID="myTextBox_AutoCompleteExtender"
     runat="server"Enabled="True" MinimumPrefixLength="1"
     ServicePath="WebService.asmx" ServiceMethod="GetCountryOrStatesInfo"
     UseContextKey="true"
     TargetControlID="myTextBox" CompletionSetCount="12" EnableCaching="true"

CompletionListCssClass="AutoExtender"             CompletionListItemCssClass="AutoExtenderList"             CompletionListHighlightedItemCssClass ="AutoExtenderHighlight"             CompletionListElementID="divwidth">
   </cc1:AutoCompleteExtender>   
    </div>
    </form>
</body>
</html>

WebService.cs
[ScriptService]
public class WebService : System.Web.Services.WebService {
    [WebMethod]
    public string[] GetCountryOrStatesInfo(string prefixText)
        {
SqlConnection con = new SqlConnection("Password=solvent;Persist Security Info=True;User ID=sa;Initial Catalog=SakshiMatrimony;Data Source=SOLVENT-007");
SqlCommand cmd=new SqlCommand ("select name from tbl_Registration Where Name like @prefixText",con);

SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand.Parameters.Add("@prefixText",SqlDbType.VarChar, 50).Value = prefixText + "%";
DataTable dt = new DataTable();
da.Fill(dt);
string[] items = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
 items.SetValue(dr[0].ToString(), i);
 i++;
 }
return items;
}
}

No comments:

Post a Comment