2015-07-13 33 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,然後它再檢查面板裏面的值在文本框中。然後我重複這種檢查其餘的面板和文本框。