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>
Shoban
  • 22,920
  • 8
  • 63
  • 107
fengd
  • 7,551
  • 3
  • 41
  • 44

3 Answers3

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();"
AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120
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>
Sid C
  • 67
  • 2
  • Thanks for your answering. But what I want to know is that is this a bug of DropDownList and Validator when posting? – fengd Dec 10 '10 at 15:22
  • the result of this code is validation, but the OP wants to achieve that the dropdownlist is not validated. – AGuyCalledGerald Jul 04 '12 at 11:07
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?

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • 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? – Joe Niland Mar 15 '11 at 05:27