0

I have a web-form in which there are three drop downs as shown below in the image:

enter image description here

The form in the above image has 3 drop-down (Physical persons responsible for operations, Resident, Staff) and my task is that if nothing is selected it should display a warning message Please Select an Instrument Type


The .aspx code which I have used in order to achieve that is:

<asp:DropDownList ID="ddlInstrumentType2" runat="server" AutoPostBack="true">
</asp:DropDownList>
<asp:Label ID="InstrumentTypeSelected" runat="server" Visible="true"></asp:Label>
<asp:Image class="helpicon" ID="imgSelInst" runat="server" ImageUrl="~/images/help2.png" ToolTip="imgSelInst"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" InitialValue="---------   Select Instrument Type   ---------" ControlToValidate="ddlInstrumentType2"
   ErrorMessage="Please select an Instrument Type" ValidationGroup="vgrp1">*
</asp:RequiredFieldValidator>

The above code doesn't seems to validate as it doesn't display any warning message if I don't select anything from the dropdown. I am wondering what mistake I am doing and what changes I need to do in the above code so that it successfully displays warning if I don't select any dropdown.

1 Answer 1

1

I expect your DropDownList doesn't have ListItem item value defined in InitialValue attribute, hence you need to use other value to trigger validation:

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" InitialValue="0" ControlToValidate="ddlInstrumentType2"
   ErrorMessage="Please select an Instrument Type" ...>*
</asp:RequiredFieldValidator>

Note: Make sure the validation group name defined in RequiredFieldValidator exists.

Or in your DropDownList (which supposed to be bound for data source) use a ListItem with initial value as first item:

<asp:DropDownList ID="ddlInstrumentType2" runat="server" AutoPostBack="true">
    <asp:ListItem Text="---------   Select Instrument Type   ---------" Value="---------   Select Instrument Type   ---------"></asp:ListItem> 
</asp:DropDownList>

Since RequiredFieldValidator control only works if ListItem is blank, the first approach should be enough.

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

1 Comment

I tried using 1st code <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" InitialValue="0" ControlToValidate="ddlInstrumentType2" ErrorMessage="Please select an Instrument Type" ValidationGroup="vgrp1">* </asp:RequiredFieldValidator> but it didn't work for some reasons.

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.