Question

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?

Was it helpful?

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;
        }
    }
}

OTHER TIPS

if you are not setting Lable visibility false anywhere else, try this

protected void TextBox1_TextChanged1(object sender, EventArgs e)
{
    dt = g1.return_dt("select name from tbl_data_show");
    if (dt.Rows.Count > 0)
    {
        if (TextBox1.Text == dt.Rows[0]["name"])
        {
            Label1.Text = "4";
            Label1.Visible = true;
        }

        else if (TextBox1.Text != dt.Rows[0]["name"])
        {
            Label2.Text = "5";
            Label2.Visible = true;
        }
        else
         {
           Label1.Visible = false;
           Label2.Visible = false;
         }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top