Thursday, August 9

Download Source Code AutoComplete TextBox Using Webservice Application

I am shibashish mohanty going to show you a simple Auto complete Application using web services.

Dopwnload Source Code From My Code Project Article

My source Code:-


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TextBoxAutoComplete.aspx.cs" Inherits="Test.LINQInsert" %>

<%@ 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>
    <script type="text/javascript">
        function setCompanyMasterID(source, eventargs) {
            document.getElementById("Label2.ClientID").value =
                                         eventargs.get_value();
            document.getElementById("Label2.ClientID").value =
       document.getElementById("TextBox1.ClientID").value;
        }
</script>
    </head>
<body>
  
    <form id="form1" runat="server">
<asp:ScriptManager runat="server"></asp:ScriptManager>
   
   
    <table >
        <tr>
            <td  >
                ID</td>
            <td>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
   
   
            </td>
        </tr>
        <tr>
            <td>
                Name</td>
            <td>
                <asp:TextBox ID="TextBox4" runat="server" AutoPostBack="True"
                    ontextchanged="TextBox4_TextChanged"></asp:TextBox>
                <asp:AutoCompleteExtender ID="TextBox4_AutoCompleteExtender" runat="server"
                    DelimiterCharacters=""
                Enabled="True" MinimumPrefixLength="1"  ServicePath="Service1.svc" ServiceMethod="TXTSEARCH" TargetControlID="TextBox4"></asp:AutoCompleteExtender>
            </td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    <p>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:TestConnectionString2 %>"
            SelectCommand="SELECT * FROM [TextboxAutoImpliment]"></asp:SqlDataSource>
    </p>
    <p>

     <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"
                   ></asp:TextBox>
                <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
                    DelimiterCharacters=""
                Enabled="True" MinimumPrefixLength="1"  ServicePath="Service1.svc" ServiceMethod="GetCountries" OnClientItemSelected="setCompanyMasterID" TargetControlID="TextBox1"></asp:AutoCompleteExtender>
          
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
          
       </p>
    </form>
</body>
</html>


My Code Behind:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Threading;
namespace Test
{
    public partial class LINQInsert : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }

        protected void TextBox4_TextChanged(object sender, EventArgs e)
        {
            DataClasses1DataContext d1 = new DataClasses1DataContext();
            var res = (from result in d1.TextboxAutoImpliments
                       where result.Name.ToLower().StartsWith(TextBox4.Text.ToLower())

                       select new { ID = result.ID }).Take(10);
            foreach (var id in res)
            {
                Label1.Text = id.ToString();
            }
        }

      
       
    }
}

My webService Method:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Configuration;
using System.Data.SqlClient;
using AjaxControlToolkit;

namespace Test
{
    /// <summary>
    /// Summary description for AutoComplete
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class AutoComplete : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [System.Web.Services.WebMethod]

        public static string[] GetSuggestions(string prefixText, int count)
        {

            try
            {



                string conStr;

                conStr = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

                string sqlQuery = "SELECT [Name],[ID] FROM [TextboxAutoImpliment] WHERE Name like @Name";



                SqlConnection conn = new SqlConnection(conStr);

                SqlCommand cmd = new SqlCommand(sqlQuery, conn);

                cmd.Parameters.AddWithValue("@Name", prefixText + "%");

                conn.Open();



                SqlDataReader dr = cmd.ExecuteReader();

                List<string> custList = new List<string>();

                string custItem = string.Empty;



                while (dr.Read())
                {

                    custItem = AutoCompleteExtender.CreateAutoCompleteItem(dr[0].ToString(), dr[1].ToString());

                    custList.Add(custItem);

                }



                conn.Close();

                dr.Close();



                return custList.ToArray();



            }

            catch (Exception ex)
            {

                throw ex;

            }

        }

    }
}
My database Look:-

Aftter Running View:-


Dopwnload Source Code From My Code Project Article

 Thanks Shibashish Mohanty

No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

.

ShibashishMnty
shibashish mohanty