隐藏在ASP.Net的ValidationSummary多余的错误信息 [英] Hide redundant error message in ASP.Net ValidationSummary

查看:185
本文介绍了隐藏在ASP.Net的ValidationSummary多余的错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我接受两个输入值的asp.net页面 - 名称和地址。他们两人都是必填字段。我已经使用必填字段验证器和验证摘要。

在发射是否两倍,虽然该错误信息是冗余的用户没有输入这两个值的错误消息。我需要即使有两个错误只显示一个错误消息。


  1. 我们如何处理它使用jQuery?

  2. 我们如何处理它使用ASP.Net标记?

注意:我最初以为验证控件会发光,而HTML页面加载自身,这样我可以用查看源文件,做的jQuery的HTML元素。但事实并非如此。它使得仅如下:

 < D​​IV ID =vsumAll级=的ValidationSummary的风格=显示:无;> < / DIV>

结果

ASP.Net标记

 <身体GT;
<表ID =form1的=服务器>
< D​​IV>
    <表格的cellpadding =5CELLSPACING =5WIDTH =100%>
        &所述; TR>
            &所述; TD>
                <表格边框=0ALIGN =中心的cellpadding =0CELLSPACING =0WIDTH =100%>
                    &所述; TR>
                        &所述; TD>
                            名称
                        < / TD>
                        &所述; TD>
                            < ASP:文本框=服务器ID =改为txtName>< / ASP:文本框>
                            < ASP:的RequiredFieldValidator =服务器ID =reqWorkorderFormat的ControlToValidate =改为txtName
                                文本=*的ErrorMessage =填充所有的值!>< / ASP:&的RequiredFieldValidator GT;
                        < / TD>
                    < / TR>
                    &所述; TR>
                        &所述; TD>
                            地址
                        < / TD>
                        &所述; TD>
                            < ASP:文本框=服务器ID =txtAddresss>< / ASP:文本框>
                            < ASP:的RequiredFieldValidator =服务器ID =RequiredFieldValidator1的ControlToValidate =txtAddresss
                                文本=*的ErrorMessage =填充所有的值!>< / ASP:&的RequiredFieldValidator GT;
                        < / TD>
                    < / TR>
                    &所述; TR>
                        &所述; TD列跨度=2>
                            < ASP:按钮=服务器ID =btnSave文本=保存/>
                        < / TD>
                    < / TR>
                    &所述; TR>
                        &所述; TD列跨度=2>
                            < ASP:的ValidationSummary =服务器ID =vsumAllDISPLAYMODE =BulletList的CssClass =的ValidationSummary/>
                        < / TD>
                    < / TR>
                < /表>
            < / TD>
        < / TR>
    < /表>
< / DIV>
< /表及GT;
< /身体GT;


解决方案

这是一个小的hackish,但它会工作:

添加以下JavaScript函数:

 函数submitValidate(){
    VAR的isValid = Page_ClientValidate('');    如果(!的isValid){
        的setTimeout($('#vsumAll UL李:没有(:第一))。remove()方法,5);
    }    返回的isValid;
}

在您的提交按钮补充一点:

 < ASP:按钮=服务器ID =btnSave文本=保存的OnClientClick =submitValidate();/>

和finaly,请确保您有的ClientIDMode =静态的ValidationSummary

说明:结果
它使用jQuery来删除所有,但在ValidationSummary第一 - 这实际上是一个UnorderedList(如 UL )。结果
我把它放在一个5ms的的setTimeout 因为我们希望它运行后,才在ValidationSummary添加完所有项目的 UL 结果
如果 Page_ClientValidate 失败,code将只运行 - 这是执行客户方的验证功能

I have a asp.net page accepting two input values – Name and Address. Both of them are required fields. I have used required field validators and validation summary.

When user does not enter both the values the error message if fired two times though the error message is redundant. I need to display only one error message even though there are two errors.

  1. How can we handle it using jQuery?
  2. How can we handle it using ASP.Net markup?

Note: I initially thought that the validation control will be emitting HTML while page load itself so that I can use "view source" and do jQuery on HTML elements. But it is not. It renders only as follows

 <div id="vsumAll" class="validationsummary" style="display:none;"> </div>

Result

ASP.Net Markup

<body>
<form id="form1" runat="server">
<div>
    <table cellpadding="5" cellspacing="5" width="100%">
        <tr>
            <td>
                <table border="0" align="center" cellpadding="0" cellspacing="0" width="100%">
                    <tr>
                        <td>
                            Name
                        </td>
                        <td>
                            <asp:TextBox runat="server" ID="txtName"></asp:TextBox>
                            <asp:RequiredFieldValidator runat="server" ID="reqWorkorderFormat" ControlToValidate="txtName"
                                Text="*" ErrorMessage="Fill all values!"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Address
                        </td>
                        <td>
                            <asp:TextBox runat="server" ID="txtAddresss"></asp:TextBox>
                            <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" ControlToValidate="txtAddresss"
                                Text="*" ErrorMessage="Fill all values!"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <asp:Button runat="server" ID="btnSave" Text="Save" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <asp:ValidationSummary runat="server" ID="vsumAll" DisplayMode="BulletList" CssClass="validationsummary" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</div>
</form>
</body>

解决方案

This is a little hackish, but it'll work:

Add the following Javascript function:

function submitValidate() {
    var isValid = Page_ClientValidate('');

    if (!isValid) {
        setTimeout("$('#vsumAll ul li:not(:first)').remove()", 5);
    }

    return isValid;
}

In your submit button add this:

<asp:Button runat="server" ID="btnSave" Text="Save" OnClientClick="submitValidate();"/>

And finaly, make sure you have ClientIDMode="Static" on your ValidationSummary

Explanation:
It uses JQuery to remove all but the first li in the ValidationSummary - which is actually an UnorderedList (e.g. ul).
I put it in a 5ms setTimeout since we want it to run only after the ValidationSummary finished adding all the items to the ul.
The code will only run if Page_ClientValidate fails - this is the function that performs the ClientSide validation.

这篇关于隐藏在ASP.Net的ValidationSummary多余的错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆