Reputation: 11669
I have a web-form in which there are three drop downs as shown below in the image:
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.
Upvotes: 0
Views: 177
Reputation: 24957
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.
Upvotes: 1