connectseekers.com

Thursday, 18 June 2015

Validate DropDownlist Using RegularExpression In Asp.Net

6/18/2015 - By Pranav Singh 0

This article will show you how you can validate the DropDownList using validating control RequiredFieldValidator in asp.net and display the error message.




After this we will make some property changes.

1.       We will define the error message in RequiredFieldValidator control which will get displayed after as the error occur

2.        Now we will define the control to RequiredFieldValidator which we are going to validate.


3.       Now we will define the InitialValue which will be the value to validate the control. In the initial value is selected on that case error message will get displayed.

After making all the changes your code will look as below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm6.aspx.cs" Inherits="WebApplication2.WebForm6" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Validate DropDownlist Using RegularExpression In Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Select Color :
        <asp:DropDownList ID="DropDownList1" runat="server" Width="100px">
            <asp:ListItem>Select</asp:ListItem>
            <asp:ListItem>Red</asp:ListItem>
            <asp:ListItem>Blue</asp:ListItem>
            <asp:ListItem>Green</asp:ListItem>
        </asp:DropDownList> <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
            runat="server" ErrorMessage="Please select color"
        Style="color: #FF0000" InitialValue="Select" ControlToValidate="DropDownList1"></asp:RequiredFieldValidator>
      
    </div>
    <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" /><br />
    <asp:Label ID="Label1" runat="server" Text="" style="color: #0033CC"></asp:Label>
    </form>
</body>
</html>

After this we will write some code to display the selected value of the dropdownlist.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "Selected Color : " + DropDownList1.SelectedItem;
        }
    }
}

Now we have done run the application and check the output.

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top