Saturday, May 5, 2012

JQuery autocomplete using asp.net and c# for dynamic textboxes

Default.aspx



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>

<!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 src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <link href="Styles/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
    <script src="js/jquery.autocomplete.js" type="text/javascript"></script>
    <script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="js/jquery.autocomplete.js" type="text/javascript"></script>
    <script src="js/jquery.maskedinput-1.3.js" type="text/javascript"></script>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>
    </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 Default7 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 5; i++)
        {
            TextBox txtSearch = new TextBox();
            txtSearch.ID = "txtSearch" + i.ToString();
            String scriptText = "<script type='text/javascript'> $(document).ready(function () { $(\"#txtSearch"+i.ToString()+"\").autocomplete('autofill.ashx').result(function (event, data, formatted) { if (data) { alert(data[1]); }  });  }); </script>";            
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Script"+i.ToString(), scriptText);
            Panel1.Controls.Add(txtSearch);
        }
    }
}

autofill.ashx

<%@ WebHandler Language="C#" Class="autofill" %>

using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Web.SessionState;

public class autofill : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        
        string name;
        string id;
       
            
            string prefixText = context.Request.QueryString["q"];
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["SurgereConnection"].ConnectionString;
            StringBuilder stbuilder = new StringBuilder();
         
                stbuilder.Clear();
                stbuilder.Append("select * from Tbl_SampleNames where Name LIKE '"+ prefixText + "%'");
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = stbuilder.ToString();
                cmd.Connection = conn;
                StringBuilder sb = new StringBuilder();
                conn.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read()) {
                    name = sdr["Name"].ToString() ;
                    id = sdr["NameId"].ToString();
                    sb.Append(string.Format("{0}|{1}|{2}", name, id, "/n"));
                    //  sb.Append(sdr("Supplier_name").ToString & "-" & sdr("Location_name").ToString) _
                    // .Append(Environment.NewLine)
                }
                conn.Close();
                if ((sb.ToString() != "")) {
                    context.Response.Write(sb.ToString());
                }
                else {
                    context.Response.Write("Not available..");
                }
           
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...