2010-05-07 38 views
0

Iam做一個簡單的網站,當我按下按鈕時,我應該看到標籤中的重量..我看到但如果text.maxlenght> 6,我應該看到ERROR消息在同一標籤,但重複看標籤重量。我怎麼解決這個問題?簡單的問題關於C#if else語句

protected void Button1_Click(object sender, EventArgs e) 
{ 
    double sayi1, sayi2, sayi3, hesap, sonuc; 

    sayi1 = Convert.ToDouble(Tb1.Text); 
    sayi2 = Convert.ToDouble(Tb2.Text); 
    sayi3 = Convert.ToDouble(Tb3.Text); 

    if (Tb1.MaxLength > 6 || Tb2.MaxLength > 6) 
    { 
     lbl1.Text = "ERROR."; 

    } 
    else 
    { 
     hesap = (((sayi1 - ((sayi1 - sayi2)/2)) * ((sayi1 - sayi2)/2))/40); 
     sonuc = (hesap * sayi3)/100; 
     lbl1.Text = sonuc.ToString() + "kg"; 
    } 


} 
+1

有作爲「C#.NET」沒有這樣的事。只有「C#」。 – 2010-05-07 23:18:33

+1

有沒有達納,只有Zool ... :-) – Pretzel 2010-05-07 23:24:18

回答

4

我想你想

Tb1.Text.Length > 6 

不是

Tb1.MaxLength > 6 
+0

非常感謝你matthew.its完成.. – ilkdrl 2010-05-07 22:46:42

1

我不完全相信你的要求,但你想了解我的內容長度文本框?

在這種情況下,你會說:

Tb1.Text.Length > 6 

Tb1.MaxLength > 6 

的MaxLength像你使用的是文本框的屬性,而不是基於輸入字段的內容。

1

問題在於對Tb1.MaxLength的引用。該屬性是一個驗證屬性,可防止用戶輸入比該值長的文本。

如果您在文本框中設置了MaxLength屬性,那麼用戶將永遠無法輸入超過6個字符。

如果你想要一個錯誤,然後檢查文本的長度,與

Tb1.Text.Length> 6

,你也可以使用double.tryparse功能,以防萬一有人不在文本框中輸入一個數值(但你可以通過驗證器來達到同樣的目的)。

0

你必須到那個標籤存儲在會話或視圖狀態變量,並設定它再次內幕您的if語句:

protected void Button1_Click(object sender, EventArgs e) { double sayi1, sayi2, sayi3, hesap, sonuc; 

    sayi1 = Convert.ToDouble(Tb1.Text); 
    sayi2 = Convert.ToDouble(Tb2.Text); 
    sayi3 = Convert.ToDouble(Tb3.Text); 

    if (Tb1.Text.Length > 6 || Tb2.Text.Length > 6) 
    { 
     lbl1.Text = "ERROR."; 
     if (session["currentTb1"] != null && session["currentTb2"] != null){ 
      Tb1.Text = session["currentTb1"].toString(); 
      Tb2.Text = session["currentTb2"].toString(); 
     } 
    } 
    else 
    { 
     hesap = (((sayi1 - ((sayi1 - sayi2)/2)) * ((sayi1 - sayi2)/2))/40); 
     sonuc = (hesap * sayi3)/100; 
     lbl1.Text = sonuc.ToString() + "kg"; 
     session["currentTb1"] = Tb1.Text; 
     session["currentTb2"] = Tb2.Text; 
    } 


} 
+0

thx但tb1.Text.Lenght更有用:D – ilkdrl 2010-05-07 22:49:46

+0

啊,我以爲你想重新載入前面的文字如果出現錯誤,請輸入框值。 – derek 2010-05-07 22:56:11