0

I have a drop down list on a page, by default the option that is displayed is 'Please Select One'.At the moment users can select that option and gain access to the next page, what I want to do is if 'Please Select One' is selected ensure that access to the next page/step will not be given until an actual option on the drop down list is selected.

Im guessing some sort of If statement but im unsure of how to do this.

Any help would be great.

This is my code for my ddl

 <td class="question">
     Out of Hours Working:
 </td>
 <td>
    <asp:DropDownList ID="ddlout" runat="server" Width="150px">
      <asp:ListItem Text="Please Select One"></asp:ListItem>
      <asp:ListItem Text="Yes"></asp:ListItem>
      <asp:ListItem Text="No"></asp:ListItem>
    </asp:DropDownList>
    <span class="mandatory">*</span>
    <asp:RequiredFieldValidator
        ID="RequiredFieldValidator14" runat="server" ControlToValidate="ddlout"
        ErrorMessage=" Required." InitialValue="Please select one..."
        ForeColor="Red" SetFocusOnError="true"></asp:RequiredFieldValidator>
 </td>
2
  • If you are adding code to your questions, please take a minute to format it, you have a preview of what it looks like just under the question text. Remember, your readers should be able to read the code and understand it without having to scroll a mile to see if something is important. Commented Sep 2, 2012 at 16:40
  • Ok I will keep that in mind for the future Commented Sep 2, 2012 at 17:16

1 Answer 1

3

Instead of using the <asp:RequiredFieldValidator> use the <asp:CompareValidator>...

<asp:CompareValidator
    ID="val14" runat="server" ControlToValidate="ddlout"
    ErrorMessage=" Required." Operator="NotEqual"
    ValueToCompare="Please Select One"
    ForeColor="Red" SetFocusOnError="true" />

Note the additional Operator and ValueToCompare. If the value of the dropdown is "not equal" to the "value to compare" then it is ok - otherwise it will fire.

See MSDN for more information

I would, however, recommend that you give actual Value's to each of the ListItem objects, rather than using the Text alone. For instance <asp:ListItem value="0" Text="Please Select One"/> which you can then test ValueToCompare="0"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.