Monday 19 November 2012

Ajax AutoCompleteExtender without using web service file in Asp.net in c#.


Hi Friends , In the previous Ajax post I had explained Ajax AutoCompleteExtender control using web service file in ap.net in c#. In this post , I have explained AutoCompleteExtender control without web service file in Asp.net using c#.
Default.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

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

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" >
            </asp:ScriptManager>

<asp:TextBox ID="txt_country" runat="server"></asp:TextBox>
                        <asp:AutoCompleteExtender ID="txt_Student_Class_Section_AutoCompleteExtender"
                                FirstRowSelected="false"     EnableCaching="false"
                                runat="server" TargetControlID="txt_country"  ServiceMethod="search_text"
                                CompletionSetCount="10" CompletionInterval="100" MinimumPrefixLength="1"
                                ShowOnlyCurrentWordInCompletionListItem="True" UseContextKey="True">
                            </asp:AutoCompleteExtender>
</div>
</form>
</body>
</html>

Default.aspx.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
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 string[] search_text(string prefixText)
    {
        SqlConnection con = new SqlConnection("Data Source
=SUSHIL-PC; Initial Catalog
=modi ;Integrated Security=true;");
        con.Open();
        SqlCommand com = new SqlCommand("select CountryName from Country where CountryName LIKE '" + prefixText + "%' ", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);
        string[] str = new string[ds.Tables[0].Rows.Count];
        int i=0;
        try
        {
            foreach(DataRow dr in ds.Tables[0].Rows)
            {
                str.SetValue(dr["CountryName "].ToString(),i);
                i++;
            }
        }
        catch{}
        finally
        {
            con.Close();
        }
        return str;


    }

}

If you really like this post , please give a comment because it will helpful who are new  comers in Asp.Net. 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.