当自动回发下拉列表触发时,验证消息仍然显示
我有一个必需的字段验证器来验证下拉列表。这个下拉列表是一个自动回发列表,它的 Causevalidation 属性设置为 false。
问题是,当我选择默认项目时,会显示验证消息,但仍然会进行回发。回发后,消息消失。
这是代码片段:
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否在 HTML 元素的 onchange 事件中看到 WebForm_DoPostBackWithOptions 方法调用,或者 __doPostBack 方法调用?前者在执行回发之前调用Page_ClientValidate(),您可以使用JS调试工具查看其所采取的路径。另外,由于它是默认验证组,是否还有其他东西会触发它?
Do you see a WebForm_DoPostBackWithOptions method call in the onchange event of the HTML element, or a __doPostBack method call? The former makes a call to Page_ClientValidate() before performing the postback, you could use a JS debugging tool to see the path its taking. Also, since its the default validation group, could something else be triggering it?
不知何故,验证器在这里混淆了一些东西。为了防止这种行为,有不同的方法:
1)您可以
在验证器上设置,这意味着它在服务器上进行验证。
如果这会产生不需要的副作用(因为验证器被其他客户端验证器“超越”),您可以执行以下操作
2)将此 javascript/jquery 函数添加到页面:
并将事件处理程序添加到 ddl:
Somehow, the validator is confusing something here. To prevent the behaviour, there are different ways:
1) You can set
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:
and an event-handler to the ddl: