2011-12-24 17 views
2

我的C#代碼如下所示。多個條件檢查單按鈕點擊

if(TextBox1.Text.Length > 5) 
    { 
    if(TextBox2.Text.Length > 5) 
    { 
    if(TextBox3.Text.Length > 5) 
    { 
    if(TextBox4.Text.Length > 5) 
    { 
    //Action to pass to the next stage. 
    } 
    else 
    { 
    error4.text = "Textbox4 value should be minimum of 5 characters."; 
    } 
    } 
    else 
    { 
    error3.text = "Textbox3 value should be minimum of 5 characters."; 
    } 
    } 

else 
{ 
error2.text = "Textbox2 value should be minimum of 5 characters."; 
} 
} 
else 
{ 
error1.text = "Textbox1 value should be minimum of 5 characters."; 
} 

1)在上述樣品中。我正在使用嵌套的If-Else概念,在按鈕單擊時,如果TextBox1值小於5,則將移動到else部分並顯示error1值,但不會檢查是否存在其他錯誤。

2)如果我改變如果條件一步一步如果條件那麼它不會對我有用,因爲只有在所有的IF條件都滿足的情況下,動作才能完成。

3)如果我使用& &運營商要檢查所有的條件我不會得到個別錯誤給每個「錯誤標籤」

如何,我可以檢查多個IF上的一個按鈕點擊的條件?

我的原代碼

if (checkavail == "available") 
     { 
      if (name.Text.Length > 0) 
      { 
       if (email.Text.Length > 5) 
       { 
        if (password1.Text.Length > 7 && password1.Text == password2.Text) 
        { 
         if (alternate.Text.Contains("@") && alternate.Text.Contains(".")) 
         { 
          if (question.Text.Length > 0) 
          { 
           if (answer.Text.Length > 0) 
           { 
            Response.Redirect("next_page.aspx"); 
           } 
           else 
           { 
            error5.Text = "Please enter your security answer"; 
           } 
          } 
          else 
          { 
           error4.Text = "Please enter your security question"; 
          } 
         } 
         else 
         { 
          error3.Text = "Invaild alternate email address"; 
         } 
        } 
        else 
        { 
         error2.Text = "Password should be minimum 8 characters and must match confirm password"; 
        } 
       } 
       else 
       { 
        error1.Text = "Email address should be minimum 6 characters"; 
       } 
      } 
      else 
      { 
       error.Text = "Please enter your name"; 
      } 
     } 
     else 
     { 
      error1.Text = "This email address is already taken. Please try another"; 
     } 

我需要重定向時的操作,滿足所有條件來完成。如果發現多個錯誤,則每個錯誤都應得到每條錯誤消息。

+0

向我們展示整個功能 – 2011-12-24 04:52:55

+0

@JoePhilllips - 已添加代碼。 – 2011-12-24 04:56:43

回答

2

謝謝大家。我發現我的回答如下

string p1, p2, p3, p4; 
     if (TextBox1.Text.Length > 5) 
     { 
      p1 = "pass"; 
      Label1.Text = ""; 
     } 
     else 
     { 
      Label1.Text = "Textbox1 value should be minimum 5 characters."; 
      p1 = "fail"; 
     } 
     if (TextBox2.Text.Length > 5) 
     { 
      p2 = "pass"; 
      Label2.Text = ""; 
     } 
     else 
     { 
      Label2.Text = "Textbox2 value should be minimum 5 characters."; 
      p2 = "fail"; 
     } 
     if (TextBox3.Text.Length > 5) 
     { 
      p3 = "pass"; 
      Label3.Text = ""; 
     } 
     else 
     { 
      Label3.Text = "Textbox3 value should be minimum 5 characters."; 
      p3 = "fail"; 
     } 
     if (TextBox4.Text.Length > 5) 
     { 
      p4 = "pass"; 
      Label4.Text = ""; 
     } 
     else 
     { 
      Label4.Text = "Textbox4 value should be minimum 5 characters."; 
      p4 = "fail"; 
     } 
     if (p1 == "pass" && p2 == "pass" && p3 == "pass" && p4 == "pass") 
     { 
      Status.Text = "All pass"; 
     } 
2

創建一個只處理錯誤消息的函數。從這個函數返回一個Enumerable。然後,你可以格式化你的if語句是這樣,它會返回一個可枚舉:

private IEnumerable GetErrors() 
{ 
    if (TextBox1.Text.Length > 5) { yield return "Textbox1 minimum bla bla"; } 
    if (TextBox2.Text.Length > 5) { yield return "Textbox2 minimum bla bla"; } 
    if (TextBox3.Text.Length > 5) { yield return "Textbox3 minimum bla bla"; } 
} 

作出這樣的處理您的非錯誤消息的邏輯,只是執行if語句,看看是否有零次失誤另一個函數或不是:

public void DoSomething() 
{ 
    var errors = GetErrors(); 
    if (errors.Count == 0) 
     Response.Redirect("next_page.aspx"); 
    else 
     error.Text = "Please fix your errors"; 
} 
+0

Void函數不返回值,類只能返回一個值。那麼,這將如何工作? – 2011-12-24 05:02:53

+0

我對你的代碼的第二部分沒有任何理解。 – 2011-12-24 05:05:13

+0

創建一個檢查錯誤並返回Enumerable的新函數。您需要重新排列所有代碼,因爲嵌套的if語句很難處理 – 2011-12-24 05:06:00