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