如何根据Textbox的TextChanged事件中给出的条件隐藏或使其可见?

时间:2014-05-05 09:53:07

标签: c# asp.net sql textbox textchanged

我的网络表单上有一个文本框。在这个文本框中我使用Jquery Auto Complete。我在我的网络表单上取了两个标签,并试图根据我在Textbox的TextChanged事件中给出的条件隐藏这个标签。但是我无法显示可见的那个。 这是我的aspx页面 -

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

我的cs页面 -

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

//此bind1()用于自动完成。

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

请指导我在哪里做错了?

2 个答案:

答案 0 :(得分:1)

如果您未在其他任何地方设置Lable可见性为false,请尝试此

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

答案 1 :(得分:1)

如果没有错,请尝试设置autopostback = true,更改你的asp标记行,

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

<强>更新

以这种方式尝试编码,

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

让我知道,如果你再次与此斗争。

使用Ajax自动完成:

最初从此link下载ajaxcontrolkit,然后使用此link添加到您的项目中,然后将此行添加到您的源页面(下面的&lt;%@ page&gt;)

<%@ 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">

代码背后:

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