找到失败的验证器asp.net

时间:2012-11-14 16:23:31

标签: c# asp.net validation logging

我有一个页面,我想记录用户未能满足相关字段要求的每条验证消息。

问题是我的回发/按钮点击永远不会发生(可能是因为客户端验证),因此记录永远不会发生在用户实际获得每个字段之前(没有验证错误)。

按钮点击事件方法:

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);
            }
        }
    }
}

任何想法,我如何记录这些消息?

修改

<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>

2 个答案:

答案 0 :(得分:3)

以下是解决方案的一部分,您可以通过调用客户端来获取validates / true false:

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

function performCheck()
{

if(Page_ClientValidate())
{

//Do something to log true/ false
}

}

答案 1 :(得分:2)

尝试将所有验证器上的EnableClientScript属性更改为false。您的所有验证仅在服务器端进行。