2009-08-26 15 views
2

我試圖要求用戶在運行搜索查詢之前在五個文本框中的至少一個文本框中輸入數據。 。在搜索頁面上至少需要一個文本框中的數據

我得到約

  1. 轉換串的錯誤信息爲布爾 2「運算符‘& &’不能被應用於類型‘字符串’和‘字符串’的操作數」

    if (txtLoginName.Text=String.Empty && 
        txtDisplayName.Text = String.Empty && 
        txtCode.Text = String.Empty && 
        txtEmailAddress.Text = String.Empty && 
        txtName.Text = String.Empty) 
    { 
    
        lblErrorMessage.Text = "At least one search criteria is required."; 
        return; 
    } 
    

回答

8

請嘗試下面的代碼。在你的示例中,你在C#中使用「=」而不是「==」。

if (txtLoginName.Text==String.Empty && 
     txtDisplayName.Text == String.Empty && 
     txtCode.Text == String.Empty && 
     txtEmailAddress.Text == String.Empty && 
     txtName.Text == String.Empty) 

另一種方式來完成同樣的事情是將使用:

if (!String.IsNullorEmpty(txtLoginName.Text) && 
     !String.IsNullorEmpty(txtDisplayName.Text) && 
     !String.IsNullorEmpty(txtCode.Text) && 
     !String.IsNullorEmpty(txtEmailAddress.Text) && 
     !String.IsNullorEmpty(txtName.Text)) 
+1

奏效。謝謝! – 2009-08-26 15:02:50

+1

打敗我吧! ;) – ZombieSheep 2009-08-26 15:05:39

1

我試圖進入()使用String.IsNullOrEmpty的習慣,而不是直接的比較器。儘管它在引擎蓋下(AFAIK)做了同樣的事情,但是當你不使用文本框時,它是一個很好的規則,但是可能爲空的字符串值。圍繞每個測試

if (String.IsNullOrEmpty(txtLoginName.Text) && 
    String.IsNullOrEmpty(txtDisplayName.Text) && 
    String.IsNullOrEmpty(txtCode.Text) && 
    String.IsNullOrEmpty(txtEmailAddress.Text) && 
    String.IsNullOrEmpty(txtName.Text)) 
{ 
} 

</2C >

0

括號將澄清,同樣,例如

if ((txtLoginName.Text == String.Empty) && 
    (txtDisplayName.Text == String.Empty)) 
{ 
} 

if ((String.IsNullOrEmpty(txtLoginName.Text)) && 
    (String.IsNullOrEmpty(txtDisplayName.Text))) 
{ 
} 
+0

我個人的觀點是,對於String.IsNullOrEmpty版本,額外的括號會使事情太多。當然,這只是我的看法。 – ZombieSheep 2009-08-27 10:32:59

+0

呃,我有一個月的時間想想,我同意! – upsidedowncreature 2009-09-29 16:34:06

相關問題