Validating a DropDown List and a ListBox using Required Field Validator Control.

vishalneeraj-24503
Posted by vishalneeraj-24503 under ASP.NET category on | Points: 40 | Views : 1079
We have to use RequiredFieldValidator InitialValue property to validate Dropdownlist and Listbox
<%--For List box--%>
<asp:ListBox ID = "lst_employee" runat = "server" Width = "200px" SelectionMode = "Multiple">
<asp:ListItem Value="1">Manoj</asp:ListItem>
<asp:ListItem Value="2">Vishal</asp:ListItem>
<asp:ListItem Value="3">Saurav</asp:ListItem>
<asp:ListItem Value="4">Tushar</asp:ListItem>
<asp:ListItem Value="5">Nitin</asp:ListItem>
<asp:ListItem Value="6">Rajesh</asp:ListItem>
<asp:ListItem Value="7">Pooja</asp:ListItem>
</asp:ListBox>

<asp:RequiredFieldValidator ID = "req_emp1" ControlToValidate ="lst_employee" InitialValue = "" runat="server" ErrorMessage = "Please select atleast one employee"></asp:RequiredFieldValidator>
</div>
<br />
<div>

<%--For Dropdown list--%>
<asp:DropDownList ID="ddl_employee" runat="server" Width="200px">
<asp:ListItem Value="0">Select</asp:ListItem>
<asp:ListItem Value="1">Manoj</asp:ListItem>
<asp:ListItem Value="2">Vishal</asp:ListItem>
<asp:ListItem Value="3">Saurav</asp:ListItem>
<asp:ListItem Value="4">Tushar</asp:ListItem>
<asp:ListItem Value="5">Nitin</asp:ListItem>
<asp:ListItem Value="6">Rajesh</asp:ListItem>
<asp:ListItem Value="7">Pooja</asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator ID="req_emp2" ControlToValidate="ddl_employee" InitialValue="0" runat="server" ErrorMessage="Please select an employee"></asp:RequiredFieldValidator>
</div>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Validate" />

Comments or Responses

Login to post response