locked
searching with textbox like google RRS feed

  • Question

  • User219039814 posted

    I have 400 records with unique roll number

    and an asp  textbox..

    my idea is when i start typing the roll number.. the text box should autmomatically show a dropdown with the series of number i began typing with..

    can some one help me..

     

    Thursday, May 3, 2018 9:32 AM

Answers

  • User632428103 posted

    Hello vijaylakshmi,

    here is it your sample with same modification who it's work :

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bindDataList();
                }
            }
    
    
            void bindDataList()
            {
    
    
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBDIVERS"].ConnectionString);
                SqlCommand cmd = null;
                con.Open();
                DataTable table1 = null;
    
                //
                string query = "SELECT item, price FROM Products";
                cmd = new SqlCommand(query, con);
                SqlDataAdapter rs = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                rs.Fill(ds);
                table1 = ds.Tables[0];
    
                Response.Write(table1.Rows.Count);    // this statement is displaying the right count of rows..
    
                for (int i = 0; i <= table1.Rows.Count - 1; i++)
                {
                    //ADD VALUES TO<datalist>.
                   //rollnos.InnerHtml = rollnos.InnerHtml + System.Environment.NewLine + string.Format("<option value='{0}'>", table1.Rows[i][0]);
                   rollnos.InnerHtml += string.Format("<option value='{0}'>", table1.Rows[i][0]);
                }

    i've just modify the query ...and the assignment to the dataList

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 4, 2018 6:42 AM
  • User-369506445 posted

    if the datalist is bind with static data. it works pretty fine.

    your code is correct, according to your answer,  your problem is about your id, if you remember my before post about id into mater page, you get it that your is be change

    for example you have

    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    
        <input list="rollnos">
    
        <datalist id="rollnos" runat="server">
        </datalist>
    
    </asp:Content>

    and when use from asp.net controls when asp.net is render add content id to other controls,

    you must change above code to below :

    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    
        <input list="MainContent_rollnos">
    
        <datalist id="rollnos" runat="server">
        </datalist>
    
    </asp:Content>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 4, 2018 6:59 AM

All replies

  • User632428103 posted

    Hello vijaylakshmi ,

    it's an autocomplete you'd you like to develop ?

    https://www.aspsnippets.com/Articles/Implement-jQuery-AutoComplete-TextBox-from-database-using-AJAX-PageMethods-in-ASPNet.aspx

    Thursday, May 3, 2018 9:56 AM
  • User219039814 posted

    i dont want to use JQUERY..  can other method prehaps

    Thursday, May 3, 2018 10:46 AM
  • User516094431 posted

    As per your need, you can implement <datalist>. For Instance:

    <input list="browsers">
    
    <datalist id="browsers">
      <option value="Internet Explorer">
      <option value="Firefox">
      <option value="Chrome">
      <option value="Opera">
      <option value="Safari">
    </datalist>

    Refer From:

    https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_datalist

    https://www.youtube.com/watch?v=IpChRqFahZ4

    Thursday, May 3, 2018 11:54 AM
  • User219039814 posted
    Hmm..but.. my option values are not static.. i need the datalist to gets its values from a database
    Thursday, May 3, 2018 12:50 PM
  • User632428103 posted

    re, ok then as said mshoaiblibra

    use dataList add runat=server on the tag

    and on pageLoad for example add a method who bind this control as this :

     DataTable dt = Get Datas From DB
                var builder = new System.Text.StringBuilder();
                for (int i = 0; i < dt.Rows.Count; i++)
                    builder.Append(String.Format("<option value='{0}'>", dt.Rows[i][0]));
    
                dataListwithRunat.InnerHtml = builder.ToString();

    Thursday, May 3, 2018 1:12 PM
  • User219039814 posted
    Yes.. i got it.. but.. on page load adding a method means?
    Thursday, May 3, 2018 1:48 PM
  • User-369506445 posted

    hi

    you can fill it below like :

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    </head>
    <body>
        <input list="browsers">
    
        <datalist id="browsers" runat="server">
        </datalist>
    
    </body>
    </html>

    code behind

    protected void Page_Load(object sender, EventArgs e)
            {
                List<string> list=new List<string>()
                {
                    "aa","bb","cc","dd"
                };
    
                 var builder = new System.Text.StringBuilder();
                foreach (var item in list)
                {
                    builder.Append("<option value='" + item + "'>");
                }
                browsers.InnerHtml = builder.ToString();
    
            }

    another sample for fetch from database

    protected void Page_Load(object sender, EventArgs e)
            {
                var builder = new System.Text.StringBuilder();
                using (var con = new SqlConnection(connectionString))
                {
                    string query = "Select * from yourTable ";
    
                    using (var cmd = new SqlCommand(query, con))
                    {
                        con.Open();
    
                        SqlDataReader dr = cmd.ExecuteReader();
                        if (dr.Read())
                        {
                            builder.Append("<option value='" + dr["yourFiledName"].ToString() + "'>");
                        }
                        con.Close();
                        con.Dispose();
    
                    }
                    
                    browsers.InnerHtml = builder.ToString();
    
                }
            }

    Thursday, May 3, 2018 2:01 PM
  • User219039814 posted
    Cool...let me try it..
    Thursday, May 3, 2018 2:30 PM
  • User219039814 posted

    this code is not working.. i tried.. 

    <input list="rollnos">

    <datalist id="rollnos" runat="server">
    </datalist>

    Connection con = new Connection();
    SqlConnection conn = null;
    SqlCommand cmd = null;
    conn = con.getConnection();
    conn.Open();
    DataTable  table1 = null;

    string query = "select regdno from STUDENT_MASTER where COLLEGE_CODE='GIMSR'";
    cmd = new SqlCommand(query, conn);
    SqlDataAdapter rs = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    rs.Fill(ds);
    table1 = ds.Tables[0];

    Response.Write(table1.Rows.Count);    // this statement is displaying the right count of rows..

    for (int i = 0; i <= table1.Rows.Count - 1; i++)
    {
    // ADD VALUES TO <datalist>.
    rollnos.InnerHtml = rollnos.InnerHtml + System.Environment.NewLine +
    String.Format("<option value='{0}'>", table1.Rows[i][0]);
    }

    Friday, May 4, 2018 3:52 AM
  • User219039814 posted

    Is it to do with my browser or something..

    Friday, May 4, 2018 4:02 AM
  • User219039814 posted

    putting the runat server.. does no good.

    if the datalist is bind with static data. it works pretty fine.

    but with database data. the search in the datalist is not coming.

    my chrome version is 60+... so i dont think it is problem with my browser.

    besides with static data the datalist pretty fine.

    Friday, May 4, 2018 4:57 AM
  • User632428103 posted

    Hello vijaylakshmi,

    here is it your sample with same modification who it's work :

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bindDataList();
                }
            }
    
    
            void bindDataList()
            {
    
    
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBDIVERS"].ConnectionString);
                SqlCommand cmd = null;
                con.Open();
                DataTable table1 = null;
    
                //
                string query = "SELECT item, price FROM Products";
                cmd = new SqlCommand(query, con);
                SqlDataAdapter rs = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                rs.Fill(ds);
                table1 = ds.Tables[0];
    
                Response.Write(table1.Rows.Count);    // this statement is displaying the right count of rows..
    
                for (int i = 0; i <= table1.Rows.Count - 1; i++)
                {
                    //ADD VALUES TO<datalist>.
                   //rollnos.InnerHtml = rollnos.InnerHtml + System.Environment.NewLine + string.Format("<option value='{0}'>", table1.Rows[i][0]);
                   rollnos.InnerHtml += string.Format("<option value='{0}'>", table1.Rows[i][0]);
                }

    i've just modify the query ...and the assignment to the dataList

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 4, 2018 6:42 AM
  • User-369506445 posted

    if the datalist is bind with static data. it works pretty fine.

    your code is correct, according to your answer,  your problem is about your id, if you remember my before post about id into mater page, you get it that your is be change

    for example you have

    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    
        <input list="rollnos">
    
        <datalist id="rollnos" runat="server">
        </datalist>
    
    </asp:Content>

    and when use from asp.net controls when asp.net is render add content id to other controls,

    you must change above code to below :

    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    
        <input list="MainContent_rollnos">
    
        <datalist id="rollnos" runat="server">
        </datalist>
    
    </asp:Content>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 4, 2018 6:59 AM