Validation Message still show up when an autopostback dropdownlist fires
Asked Answered
B

3

4

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>
Blackbeard answered 8/12, 2010 at 20:39 Comment(1)
Thanks everyone, I ended with do the validation through purely javascript.Blackbeard
B
0
<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>
Brundisium answered 8/12, 2010 at 22:28 Comment(2)
Thanks for your answering. But what I want to know is that is this a bug of DropDownList and Validator when posting?Blackbeard
the result of this code is validation, but the OP wants to achieve that the dropdownlist is not validated.Margueritamarguerite
C
0

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?

Cultrate answered 24/12, 2010 at 4:53 Comment(1)
I also have this problem - tried to use Firebug to see what __doPostBack is doing but I couldn't seem to work it out. Any other suggestions?Cantu
M
0

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();"
Margueritamarguerite answered 4/7, 2012 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.