3
  <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

Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62

3 Answers3

5

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" 
Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62
2

Edited answer's Amarnath Balasubramanian (I have not permissions to add comments)

DropDownList

<asp:DropDownList ID="ddl" runat="server"
                  ValidationGroup="CreateRolls"
                  AppendDataBoundItems="true">
  <asp:ListItem Text="-Select-" Value="-1"></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="-1"
                            ForeColor="Red"
                            ValidationGroup="CreateRolls" >
</asp:RequiredFieldValidator>

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

ControlToValidate="ddl" 
InitialValue="-Select-"
ValidationGroup="CreateRolls"

Note that changes are

<asp:ListItem Text="-Select-" Value="-1"></asp:ListItem>
InitialValue="-1"
David López
  • 366
  • 3
  • 12
1
   <asp:DropDownList runat="server" ID="ddRoleType">
<asp:ListItem Text="-Select-" Value="-1"></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="CompareValidator1" runat="server" Text="Error" 
ControlToValidate="ddRoleType" Operator="NotEqual" ValueToCompare = "-1"
ValidationGroup="CreateRolls"></asp:CompareValidator>

or you can use Required field validator

Nimmi
  • 680
  • 8
  • 18