2012-11-14 29 views
8

我有一個頁面,我想記錄用戶未能滿足相關字段要求的每條驗證消息。查找失敗的驗證器asp.net

問題是我的回發/按鈕點擊永遠不會發生(也許是因爲客戶端驗證),因此記錄從未發生之前,用戶實際得到了各個領域的權利(無驗證錯誤)。

按鈕點擊事件方法:

protected void btnNext_Click(object sender, EventArgs e) 
{ 
    Page.Validate(); 
    if(Page.IsValid) 
    { 
     //code 
    } 
    else 
    { 
     foreach (IValidator validator in Validators) 
     { 
      if (!validator.IsValid) 
      { 
       PageValidatorErrors error = new PageValidatorErrors 
           { 
            WebsiteID = AppState.WebsiteID, 
            Page = Request.Url.AbsolutePath, 
            URL = Request.Url.ToString(), 
            UserIP = Tools.GetIP(), 
            ErrorMessage = validator.ErrorMessage, 
            CreatedDate = DateTime.Now 
           }; 
       pageValidatorErrorsRep.insert(error); 
      } 
     } 
    } 
} 

任何想法,我怎麼可以登錄theese消息?

編輯:

<script type="text/javascript"> 
    function validatePage() 
    { 
     if (window.Page_IsValid != true) 
     { 
      //Page_Validators is an array of validation controls in the page. 
      if (window.Page_Validators != undefined && window.Page_Validators != null) 
      { 
       //Looping through the whole validation collection. 
       for (var i = 0; i < window.Page_Validators.length; i++) 
       { 
        window.ValidatorEnable(window.Page_Validators[i]); 

        //if condition to check whether the validation was successfull or not. 
        if (!window.Page_Validators[i].isvalid) 
        { 
         var errMsg = window.Page_Validators[i].getAttribute('ErrorMessage'); 
         alert(errMsg); 
        } 
       } 
      } 
     } 
    } 
</script> 
+0

哦,我明白了,你是問如何連接到客戶端asp.net驗證器發出的端JavaScript。 – MatthewMartin

+0

感謝您的驗證環路樣本,我正要去寫我自己的:) – rdmptn

回答

3

這裏是解決方案的一部分,你可以通過調用它的客戶端得到生效/真假:

http://razeeb.wordpress.com/2009/01/11/calling-aspnet-validators-from-javascript/

function performCheck() 
{ 

if(Page_ClientValidate()) 
{ 

//Do something to log true/ false 
} 

} 
+0

這可能是訣竅,你知道我是如何得到「失敗」的驗證器,或者我可以以某種方式調用C#方法嗎? – KLIM8D

+0

下面是驗證器使用的JS庫的MSDN文檔的鏈接,從我的簡短閱讀中,您沒有太多工作要做:http://msdn.microsoft.com/en-us/library/ aa338815(v = vs.71).aspx – MatthewMartin

+0

謝謝,我現在可以循環驗證器並在其無效時發出警報。現在只需要提取錯誤消息,出於某種原因,每個驗證器都是空的。 我已將javascript添加到主題中。 – KLIM8D

2

試圖改變所有驗證爲false EnableClientScript屬性。你所有的驗證只會在服務器端發生。

+0

是的,這使得回發工作,如何隨着您鍵入或點擊下一個領域的字段驗證功能已經不存在了。如果可能,我想保留這一點。 – KLIM8D

+0

我認爲這可能是最好的選擇。擺脫頁面驗證器,並做到所有服務器端。這樣你就可以更好地控制記錄什麼。 – NotMe