Click here to Skip to main content
16,020,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a DropDownList and a RequiredFieldValidator that checks if a
value has been selected for the DropDownList by the user.

I am populating the drop down at runtime and adding two items, ( "Please select" and "All").

The "Please select" option has a value of "".

The problem I am facing is that when I select the "Please select"
option the error message of the RequiredFieldValidator gets displayed
for a second or so and then the page posts back.

I wanted that the error message should be displayed and the page should not post back.

Am I doing something wrong here?

I could check the value of the selected item at the server side but that is just a waste of one round trip. The AutoPostBack property of the DropDownList is set to true.

Any help would be appreciated .

Thanks in advance.
Posted
Updated 29-Oct-10 21:32pm
v2
Comments
saini arun 30-Oct-10 3:33am    
Minor formatting changes.

You can use compare validator.
Example:

XML
<asp:CompareValidator
                          id="cvNumberOfGuests"
                          runat="server"
                          ControlToValidate="ddlNumberOfGuests"
                          ErrorMessage="You must select the number of guests (1-5)."
                          ValueToCompare="0"
                          display="Dynamic"
                          Type="Integer"
                          Operator="GreaterThan">*</asp:CompareValidator>
                <asp:DropDownList ID="ddlNumberOfGuests" Width="100%" runat="server">
                    <asp:ListItem Value="0" Selected="true">Number of guests</asp:ListItem>
                    <asp:ListItem Value="1">1</asp:ListItem>
                    <asp:ListItem Value="2">2</asp:ListItem>
                    <asp:ListItem Value="3">3</asp:ListItem>
                    <asp:ListItem Value="4">4</asp:ListItem>
                    <asp:ListItem Value="5">5</asp:ListItem>
                    <asp:ListItem Value="6">Contact us for more than 5 guest</asp:ListItem>
                </asp:DropDownList>
 
Share this answer
 
v2
suppose you are having this items in your dropdownlist..

DropDownList1.Items.Add(new ListItem("--Select--","0"));
DropDownList1.Items.Add(new ListItem("Hiren","1"));
DropDownList1.Items.Add(new ListItem("Mohini","2"));
DropDownList1.Items.Add(new ListItem("Chris", "3"));


then.. set the requiredfieldvalidator's controltovalidate property to "DropDownlist1" and set the "InitialValue" property of the requiredfieldvalidator to "0" as this will be the value for "--Select--" you can show above..

hope it helps.
please let us know if you are having some another doubt than this one..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900