【问题标题】:when click on empty asp:textbox I want to set the visibility of a asp:label to false immediately当单击空 asp:textbox 时,我想立即将 asp:label 的可见性设置为 false
【发布时间】:2015-02-15 22:55:59
【问题描述】:

当点击空的 asp:textbox 时,我想立即将 asp:label 的可见性设置为 false 我将一些标签设置为验证检查 当文本框为空时,标签变为可见,但当文本更改或单击文本框进行编辑时,它不是不可见的 请帮帮我

代码:

 protected void Button1_Click(object sender, EventArgs e)
{
    if (TextBox1.Text == "")
        Label1.Visible = true;

}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    Label1.Visible = false;

}

【问题讨论】:

  • 你自己试过什么?
  • protected void Button1_Click(object sender, EventArgs e) { if (TextBox1.Text == "") Label1.Visible = true; } protected void TextBox1_TextChanged(object sender, EventArgs e) { Label1.Visible = false; }
  • 显示您的代码到目前为止您尝试过的内容
  • 在你的文本框上使用 jquery focus() 事件并在客户端顺利完成所有事情会更好吗?
  • 使用回发来处理这是一个非常糟糕的主意。而是使用简单的 javascript 来避免往返服务器。

标签: c# asp.net textbox textchanged


【解决方案1】:

您可以使用 JQuery 立即隐藏您的标签(无需重新加载页面)。您的文本框和标签有 ID,您可以使用 asp.net 4 中的 ClientID 属性获取它们。

$(document).ready(function(){
    var textbox = $('#<%=textboxId.ClientID%>');
    var label = $('#<%=labelId.ClientID%>');

    textbox.click(function(){
        if(textbox.val().length == 0){
            label.hide();
        }
    });
}); 

【讨论】:

    【解决方案2】:

    确保在 TextBox1 上保留 AutoPostBack="true"

    protected void Button1_Click(object sender, EventArgs e)
    {
            if (TextBox1.Text == "")
                Label1.Visible = true;
    
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
            if(TextBox1.Text != string.empty)
            {
                Label1.Visible = false;
            }
    
    }
    

    但还有其他有效的方法来验证相同的内容。 例如。 ASP 验证器, jQuery 验证器

    【讨论】:

      【解决方案3】:

      在这里尝试使用jquery

      这里的标签可见性会根据文本框值的变化而变化。

      代码

      $(function () {
          $('#<%= txtbox1.ClientID%>').on('change keyup paste', function () {
              if ($('#<%= txtbox1.ClientID%>').val().length == 0) {
                  $('#<%= lblTest.ClientID%>').css("visibility", "hidden");
              }
              else {
                  $('#<%= lblTest.ClientID%>').css("visibility", "visible");
              }
          });
      });
      

      【讨论】:

        猜你喜欢
        • 2016-03-17
        • 2015-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多