【问题标题】:Check if conditionally hidden textboxes have values检查有条件隐藏的文本框是否有值
【发布时间】:2015-07-13 09:37:25
【问题描述】:

我有一个是/否问题列表,每个问题都有一个指示答案的单选按钮。当用户选择“是”时,panel 将可见,并且其中包含用于额外所需输入的文本框。当用户回答“是”时,他们必须填写出现的文本框。

目前我正在以这种方式对其进行硬编码:

            if (txtQ1Specify.Visible == true)
            {
                if (txtQ1Specify.Text.Length == 0)
                {
                    lblError.Text = "Please answer all questions.";
                }
            }
            if (txtQ2Specify.Visible == true)
            {
                if (txtQ2Specify.Text.Length == 0)
                {
                    lblError.Text = "Please answer all questions.";
                }
            }
            if (txtQ3Specify.Visible == true)
            {
                if (txtQ3Specify.Text.Length == 0)
                {
                    lblError.Text = "Please answer all questions.";
                }
            }
            if (txtQ4SpecifyCompany.Visible == true || txtQ4SpecifyRelative.Visible == true)
            {
                if (txtQ4SpecifyCompany.Text.Length == 0 || txtQ4SpecifyRelative.Text.Length == 0)
                {
                    lblError.Text = "Please answer all questions.";
                }
            }
            if (txtQ5SpecifyCompany.Visible == true || txtQ5SpecifyRelative.Visible == true)
            {
                if (txtQ5SpecifyCompany.Text.Length == 0 || txtQ5SpecifyRelative.Text.Length == 0)
                {
                    lblError.Text = "Please answer all questions.";
                }
            }
            if (txtQ6Specify.Visible == true)
            {
                if (txtQ6Specify.Text.Length == 0)
                {
                    lblError.Text = "Please answer all questions.";
                }
            }
            if (txtQ7Specify.Visible == true)
            {
                if (txtQ7Specify.Text.Length == 0)
                {
                    lblError.Text = "Please answer all questions.";
                }
            }

检查后我想执行一些代码。

页面如下所示:

如何根据可见性检查文本框输入?

【问题讨论】:

  • 不清楚你在问什么。

标签: c# asp.net textbox panel


【解决方案1】:

您可以使用 LINQ 来确定是否有任何可见的和空的 TextBox,如下所示:

var query =
    from t in Page.Controls.OfType<TextBox>()
    where t.Visible && t.Text == ""
    select t;

bool hasUnanswered = query.Any();

【讨论】:

    【解决方案2】:

    这可以在客户端轻松完成。

    1. 首先您需要确定哪些所有文本框都是可见的。为此,您可以使用 jquery Visible Selector

    2. 现在如果有多个文本框可见。向用户显示一些消息并聚焦并突出显示需要填充的文本框。

    $(document).ready(function(){
        var visibleCount =$('input[type="text"]:visible').length;
      if (visibleCount  > 0)
      {
         // Add your logic here
       }
    });
    

    【讨论】:

    • 感谢您的客户端验证。但我正在寻找服务器端,对不起
    【解决方案3】:

    我设法使用很长的if 语句来做到这一点。这里什么都没有:

    if ((pnlQ1Yes.Visible == true && txtQ1Specify.Text.Length == 0) ||
                (pnlQ2Yes.Visible == true && txtQ2Specify.Text.Length == 0) ||
                (pnlQ3Yes.Visible == true && txtQ3Specify.Text.Length == 0) ||
                (pnlQ4Yes.Visible == true && (txtQ4SpecifyCompany.Text.Length == 0 || txtQ4SpecifyRelative.Text.Length == 0)) ||
                (pnlQ5Yes.Visible == true && (txtQ5SpecifyCompany.Text.Length == 0 || txtQ5SpecifyRelative.Text.Length == 0)) ||
                (pnlQ6Yes.Visible == true && txtQ6Specify.Text.Length == 0) ||
                (pnlQ7Yes.Visible == true && txtQ7Specify.Text.Length == 0))
    {
        lblError.Text = "Please answer all questions.";
    }
    else   
    {
        ...
    }
    

    它首先检查面板是否为visible,然后检查面板内的文本框的值。然后我对其余的面板和文本框重复这种检查。

    【讨论】:

      猜你喜欢
      • 2010-12-06
      • 2018-07-16
      • 2022-01-04
      • 1970-01-01
      • 2020-05-22
      • 2023-03-12
      • 1970-01-01
      • 2010-11-14
      • 2016-10-29
      相关资源
      最近更新 更多