验证消息仍然显示时自动回发的DropDownList火灾 [英] Validation Message still show up when an autopostback dropdownlist fires

查看:110
本文介绍了验证消息仍然显示时自动回发的DropDownList火灾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个必填字段验证器来验证一个DropDownList。这DropDownList的是一个自动回发一个,它的causevalidation属性设置为是假的。

问题是,当我选择了默认项,验证消息显示,但还是做回发。而回发后,消息disappers。

这里是codeS中的片段:

 < ASP:的RequiredFieldValidator ID =ContactMethodRequired=服务器的ControlToValidate =联系preferences
            显示=动态的ErrorMessage =请选择您的联系方式
            EnableClientScript =真与InitialValue =>< / ASP:&的RequiredFieldValidator GT;
        < D​​IV>
            < ASP:DropDownList的ID =联系preferences=服务器的AutoPostBack =真的CausesValidation =false的>
                < ASP:ListItem的文本=选择VALUE =选定=真>< / ASP:ListItem的>
                < ASP:ListItem的文本=电子邮件值=电子邮件>< / ASP:ListItem的>
                < ASP:ListItem的文本=电话VALUE =电话>< / ASP:ListItem的>
            < / ASP:DropDownList的>
        < / DIV>


解决方案

不知何故,验证在这里混淆的东西。以prevent行为,有不同的方式:

1)可以设置

  EnableClientScript =假

在验证器,这意味着它验证在服务器上。

如果这已不期望的副作用(因为验证器是通过其他客户端验证器取代),你可以做到这一点。

2)这个JavaScript / jQuery的功能添加到页面:

 函数HideValidator(){     VAR验证= $('#<%= ContactMethodRequired.ClientID%GT;');
     validator.hide();
}

和事件处理程序的DDL:

 的onchange =HideValidator();

I have a required field validator to validate a dropdownlist. this dropdownlist is an autopostback one, and it's causevalidation property is set to be false.

the issue is, when I select the default item, the validation message shows, but the still do the postback. And after the postback, the message disappers.

here is the snippet of codes:

<asp:RequiredFieldValidator ID="ContactMethodRequired" runat="server" ControlToValidate="ContactPreferences"
            Display="Dynamic" ErrorMessage="Please choose your contact method"
            EnableClientScript="true" InitialValue=""></asp:RequiredFieldValidator>
        <div>
            <asp:DropDownList ID="ContactPreferences" runat="server" AutoPostBack="true" CausesValidation="false">
                <asp:ListItem Text="Select" Value="" Selected="True"></asp:ListItem>                         
                <asp:ListItem Text="Email" Value="Email"></asp:ListItem>
                <asp:ListItem Text="Phone" Value="Phone"></asp:ListItem>
            </asp:DropDownList>
        </div>

解决方案

Somehow, the validator is confusing something here. To prevent the behaviour, there are different ways:

1) You can set

EnableClientScript="false"

on the validator, meaning it validates on the server.

If this has undesired side effects (because the validator is "overtaken" by other client validators), you can do this

2) add this javascript/jquery-function to the page:

function HideValidator() {

     var validator = $('#<%= ContactMethodRequired.ClientID %>');
     validator.hide();
}

and an event-handler to the ddl:

onchange="HideValidator();"

这篇关于验证消息仍然显示时自动回发的DropDownList火灾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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