Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate dropdown when value is set to empty using asp.net web form

Tags:

c#

asp.net

vb.net

  <asp:DropDownList runat="server" ID="ddl">
    <asp:ListItem Text="-Select-" Value=""></asp:ListItem>
    <asp:ListItem Text="One" Value="1"></asp:ListItem>
    <asp:ListItem Text="Two" Value="2"></asp:ListItem>
    <asp:ListItem Text="Three" Value="3"></asp:ListItem>
  </asp:DropDownList>

<asp:CompareValidator ID="cvddl" runat="server" Text="Error" 
ControlToValidate="ddl" Operator="NotEqual" ValueToCompare = ""
ValidationGroup="CreateRolls"></asp:CompareValidator>

I do want to validate dropdown, if select is selected error should be thrown. Main aim is that the value should be empty.

Is there is any methord to validate like this . Please help me with this

like image 833
Vignesh Marteen Avatar asked Feb 21 '26 23:02

Vignesh Marteen


1 Answers

DropDownList

<asp:DropDownList ID="ddl" runat="server"
                  ValidationGroup="CreateRolls"
                  AppendDataBoundItems="true">
  <asp:ListItem Text="-Select-" Value=""></asp:ListItem>
  <asp:ListItem Text="One" Value="1"></asp:ListItem>
  <asp:ListItem Text="Two" Value="2"></asp:ListItem>
  <asp:ListItem Text="Three" Value="3"></asp:ListItem>
</asp:DropDownList>

RequiredFieldValidator

<asp:RequiredFieldValidator ID="rfvDDL" runat="server"
                            ControlToValidate="ddl" 
                            Display="Dynamic"
                            ErrorMessage="Values is required."
                            InitialValue="-Select-"
                            ForeColor="Red"
                            ValidationGroup="CreateRolls" >
</asp:RequiredFieldValidator>

The Important property to be noted in the code is the following

 ControlToValidate="ddl" 
InitialValue="-Select-"
ValidationGroup="CreateRolls" 
like image 89
Amarnath Balasubramanian Avatar answered Feb 24 '26 13:02

Amarnath Balasubramanian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!