2015-07-13 92 views
0

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

目前我硬编码这样说:

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

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

的页面看起来是这样的:

screenshot

我如何检查基于文本框的可见性投入?

+0

不清楚你在问什么。 – Mairaj

回答

1

你可以使用LINQ,以找出是否有任何可见和空文本框,如下所示:

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

bool hasUnanswered = query.Any(); 
+0

这没有奏效。 –

0

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

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

  2. 现在如果有多个文本框可见。向用户显示一些消息,然后突出显示需要填写的文本框。

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

感谢您的客户端验证。但是我在寻找服务器端,对不起 –

0

我设法用了很长的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,然后它再检查面板里面的值在文本框中。然后我重复这种检查其余的面板和文本框。