This article will show you how you can validate the DropDownList
using validating control RequiredFieldValidator in asp.net and
display the error message.
Some of my previous articles are as follows: Confirmation
Message With Yes, No Button In Asp.Net Using ConfirmButtonExtender and
ModalPopupExtender, Watermark
In Asp.Net TextBox, Add
JavaScript Function To Button Control in Asp.net Using C#, Autocomplete
Textbox in Asp.Net With DataBase Using C# AjaxControlToolkit, Simple
Login Form In Asp.Net Using C#.Net and VB.Net | How to Create Login Form in
Asp.Net Using C#, Ajax
FileUpload Control In Asp.Net or Multiple FileUpload With Progress Example in
Asp.Net Using C#.Net.
So for this article first we will create a new asp.net application and add dropdownlist , RequiredFieldValidator, button control and a label control. After adding control your form will look as shown below.
So for this article first we will create a new asp.net application and add dropdownlist , RequiredFieldValidator, button control and a label control. After adding control your form will look as shown below.
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.
0 comments:
Please let me know your view