问题
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>
回答1:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript" >
function ValidPage()
{
if (typeof(Page_ClientValidate) == 'function')
{
if (typeof (Page_ClientValidate) == 'function') { Page_ClientValidate(); }
if (!Page_IsValid)
{
return false;
}
return Page_IsValid;
}
return true;
}
</script>
</head>
<body >
<form id="form1" runat="server" onsubmit="return ValidPage();" >
<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>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ContactPreferences"
Display="Dynamic" ErrorMessage="Please choose your contact method"
EnableClientScript="true" InitialValue=""></asp:RequiredFieldValidator>
</div>
</form>
</body>
</html>
回答2:
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?
回答3:
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();"
来源:https://stackoverflow.com/questions/4392079/validation-message-still-show-up-when-an-autopostback-dropdownlist-fires