AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Wednesday, 16 March 2016

How add ASP.NET required validation on Drop down field?

In this blog post I will discuss how to add a required field validation in Drop down fields. Usually we use required field validation with text box easily, but we can also use it with drop downs lets discuss how doing it.
Step 1- Add drop down control & button on a aspx page.

<asp:DropDownList ID="ddlMerchant" CssClass="form-control" runat="server">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="valgrp" ControlToValidate="ddlMerchant" InitialValue="0" runat="server" ForeColor="Red" Display="Dynamic" ErrorMessage="Please Select Merchant."></asp:RequiredFieldValidator>

Here validation group is set as "valgrp". As the drop down selected value is 0 then it shows error message to select dropdown.

Step 2 -Next add a button to check validation is working or not.
<asp:Button ID="btnAddMerchant" ValidationGroup="valgrp" CssClass="btn btn-primary" Text="Add Merchant" runat="server" OnClick="btnAddMerchant_Click" />

As you click on button it will show required field validation error.


Post a Comment

  • Popular Posts
  • Comments