Validation Message still show up when an autopostback dropdownlist fires

你离开我真会死。 提交于 2019-12-10 15:46:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!