Search code examples
c#asp.netsqltextboxtextchanged

How to hide or make it visible based on the condition given in the TextChanged event of the Textbox?


I am having a textbox on my web form. Within this textbox I am using Jquery Auto Complete. I have taken two labels on my web form and trying to hide this label based on the condition I have given in the TextChanged event of the Textbox. But I am not able display the visible one. This is my aspx page-

<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <script type="text/javascript">
      $(function () {
      var items=[<%=autotag %>];
      $("#TextBox1").autocomplete({
      source:items
      });
      });
  </script>
<table>
<tr>
<td>Name:</td>
<td>
    <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged1" 
        AutoPostBack="True"></asp:TextBox></td>
</tr>
   <tr><td colspan="2">
<asp:Panel ID="Panel1" runat="server" Visible="False">
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    <br />
    <asp:Label ID="Label2" runat="server" Text=""></asp:Label></asp:Panel>
    </td></tr>
</table>

My cs page-

public string autotag="";
protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        bind1();
    }
}

//This bind1() is for autocomplete.

public void bind1()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString);
con.Open();
string query="select name from tbl_data_show";
SqlCommand cmd=new SqlCommand(query,con);
SqlDataReader dr=cmd.ExecuteReader();
dr.Read();
while(dr.Read())
{
    if(string.IsNullOrEmpty(autotag))
    {
        autotag+="\""+dr["name"].ToString()+"\"";
    }
    else
    {
        autotag+=", \""+dr["name"].ToString()+"\"";
    }
}
}
protected void TextBox1_TextChanged1(object sender, EventArgs e)
{   
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("select name from tbl_data_show where name='"+TextBox1.Text+"'", con);
    //DataTable dt1 = new DataTable();
    //SqlDataAdapter da = new SqlDataAdapter(cmd);
    //da.Fill(dt1);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
        while (dr.Read())
        {
            Panel1.Visible = true;
        }
    }
    else
    {
        Panel1.Visible = false;
    }
    con.Close();
}

Please guide me where I am doing wrong?


Solution

  • If am not wrong, please try by setting autopostback = true, change your asp tag line like this,

     <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged1" AutoPostBack="True"></asp:TextBox>
    

    Update

    Try your coding in this way,

       protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {     
          bind1();
          //Panel1.Visible = true;
        }
    }
    
     protected void TextBox1_TextChanged1(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("select name from tbl_data_show where name='"+TextBox1.Text+"'", con);
       // DataTable dt1 = new DataTable();
       // SqlDataAdapter da = new SqlDataAdapter(cmd);
       // da.Fill(dt1);
       SqlDataReader dr = cmd.ExecuteReader();
       if (dr.HasRows)
       {
           while (dr.Read())
           {
               Panel1.Visible = true;
               //if (dt1.Rows.Count > 0)
               //{
               //    if (TextBox1.Text == dr.GetString(0))
               //    {
               //        //Label1.Text = "4";
               //        //Label2.Text = "5";
               //        Panel1.Visible=true;
               //        //Label1.Visible = true;
               //        //Label2.Visible = false;
               //    }
               //    else
               //    {
               //        //Label2.Text = "5";
               //        Panel1.Visible = false;
               //        //Label2.Visible = true;
               //        //Label1.Visible = false;
               //    }
               //}
           }
       }
       else
       {
           Panel1.Visible = false;
       }
        con.Close();
    }
    

    Let me know, if you get struggle with this again.

    Autocomplete Using Ajax :

    Initially download ajaxcontrolkit from this link, and then add into your project using this link and then add this line into your source page (below to <%@ page>)

    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    

    aspx :

    <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged1" 
        AutoPostBack="True"></asp:TextBox>
         <asp:ScriptManager ID="ScriptManager1" runat="server">
                </asp:ScriptManager>
                  <asp:AutoCompleteExtender ServiceMethod="SearchCustomers" 
    MinimumPrefixLength="1"
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" 
    TargetControlID="TextBox1"
    ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
    

    Code Behind :

     protected void Page_Load(object sender, EventArgs e)
    {
        //bind1();
    }
    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> SearchCustomers(string prefixText, int count)
    {
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager
                    .ConnectionStrings["conn"].ConnectionString;
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select name from tbl_data_show where " +
                "name like @SearchText + '%'";
                cmd.Parameters.AddWithValue("@SearchText", prefixText);
                cmd.Connection = conn;
                conn.Open();
                List<string> customers = new List<string>();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        customers.Add(sdr["name"].ToString());
                    }
                }
                conn.Close();
                return customers;
            }
        }
    }