ARTICLE

How to validate asp dropdownlist in VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET April 14, 2011
This Article defines that how to validate asp dropdownlist in VB.NET.
 
Reader Level:

This Article defines that how to validate asp dropdownlist in VB.NET.

In this article we Drag and Drop one Dropdownlist and a Button control on the form and also drag a RequiredFieldValidator on the form to validate dropdownlist. Here when we select a item from the DropDownList and click on the Button control it will work fine. But when we will click on the Button control with out select items from the DropDownList it will show error.

For example

Now Drag and Drop one Dropdownlist and a Button control on the form and also drag a RequiredFieldValidator on the form to validate dropdownlist. The form looks like this.

ddl.gif

Figure1

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication29.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        If Page.IsValid Then

            lblResult.Text = dropFavoriteColor.SelectedValue

        End If

    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title>Show Initial Value</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:Label

        id="lblFavoriteColor"

        Text="Favorite Color:"

        AssociatedControlID="dropFavoriteColor"

        Runat="server" />

    <br />

    <asp:DropDownList

        id="dropFavoriteColor"

        Runat="server">

        <asp:ListItem Text="Select Color" Value="none" />

        <asp:ListItem Text="Red" Value="Red" />   

        <asp:ListItem Text="Blue" Value="Blue" />   

        <asp:ListItem Text="Green" Value="Green" />   

    </asp:DropDownList>

    <asp:RequiredFieldValidator

        id="reqFavoriteColor"

        Text="Please select items from DropDownList"

        InitialValue="none"

        ControlToValidate="dropFavoriteColor"

        Runat="server" ErrorMessage="Please select items from DropDownList" />

    <br /><br />

    <asp:Button

        id="btnSubmit"

        Text="Submit"

        Runat="server" OnClick="btnSubmit_Click" />

    <hr />

    <asp:Label

        id="lblResult"

        Runat="server" />

    </div>

    </form>

</body>

</html>

 

Now run the application and test it.

ddl1.gif

Figure2

Now we will click on the Button control with out select items from the DropDownList it will show error.

ddl2.gif

Figure3

Now we select a item from the DropDownList and click on the Button control it will work fine.

ddl3.gif

Figure4

Login to add your contents and source code to this article
share this article :
post comment
 

useful article

Posted by Shalini Juneja Apr 15, 2011
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Diagram
Become a Sponsor