2012-07-25 21 views
3

我試圖將記錄添加到sharepoint 2010的「鏈接」列表中。 我想要添加項目驗證。我是這樣寫的。 enter image description here在將記錄添加到sharepoint 2010中的列表時檢查條件時出現「空引用異常」

我試圖停止輸入任何包含谷歌關鍵字的鏈接。下面的代碼中是否有任何錯誤。我正在爲我的練習而努力。 我認爲在圖像代碼中顯示不清晰。我在這裏寫下。

if (properties.AfterProperties["vti_url"].ToString().Contains("google")) 
      { 
       properties.ErrorMessage = "You should not enter the google"; 
       properties.Cancel = true; 
      } 

添加properties.AfterProperties["URL"].ToString().Contains("google")後,它運行良好。但是我的錯誤頁面看起來不太好。這裏是屏幕截圖。該怎麼做這個問題。 enter image description here

+0

如果有'屬性,你檢查。 AfterProperties [「vti_url」]'返回'null'? – Hinek 2012-07-25 07:35:43

回答

1

我會採取胡亂猜測,說properties.AfterProperties["vti_url"]null,因爲沒有現場所謂的「vti_url」 ......你試過:

properties.AfterProperties["URL"] 
+0

你好Hinek ..謝謝你的回答。我認爲這是接近工作。請看我編輯的問題。 – Searcher 2012-07-25 08:46:25

+0

嗨,爲您的新錯誤,這可能有所幫助:http://sharepoint.stackexchange.com/questions/28495/event-reciever-custom-error-message-page-sharepoint-list-2010 – Hinek 2012-07-25 08:51:15

+0

在該鏈接中,接受答案是恢復對web.config文件所做的更改。但是我沒有對web.config文件進行任何更改。屬性值只是相同的 – Searcher 2012-07-25 09:01:11

1

enter code here提到的評論Hinek是更可能正確

/// <summary> 
    /// Checks the object to see if it's null, if it isn't it will return the string value of the object. 
    /// </summary> 
    /// <param name="value">(object) The object you want to check.</param> 
    /// <returns>(string) The string value of the object.</returns> 
    public static string CheckForNullValue(object value) 
    { 
     string tmpValue = string.Empty; 
     try 
     { 
      if (value != null) 
      { 
       if (!string.IsNullOrEmpty(value.ToString())) 
       { 
        tmpValue = value.ToString(); 
       } 
      } 
     } 
     catch (Exception exc) 
     { 
      Error.Log("Failed to check for null value Value passed in" + value, exc, Error.ErrorType_Error); 
      throw exc; 
     } 
     return tmpValue; 
    } 

string url = CheckForNullValue(properties.AfterProperties["vti_url"]) 


if (url.Contains("Google")) 
     { 
      properties.ErrorMessage = "You should not enter the google"; 
      properties.Cancel = true; 
     } 

希望這有助於

相關問題