How To Use RequiredFieldValidator control with DropDownList control For Validation http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=252&title=How-To-Use-RequiredFieldValidator-control-with-DropDownList-control-For-Validation
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm7.aspx.cs" Inherits="WebApplication1.WebForm7" %>
<!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></title> </head> <body> <form id="form1" runat="server">
<div> <asp:DropDownList ID="DropDownList1" runat="server" Width="200px"> </asp:DropDownList><asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="DropDownList1" runat="server" ErrorMessage="Please select item."></asp:RequiredFieldValidator> </div> <br /> <asp:button runat="server" text="Submit" onclick="Unnamed1_Click" /> <br /> <asp:Label ID="Label1" runat="server" style="color: #FF3300" Text=""></asp:Label> </form> </body> </html>
Now in your .cs page add the below code .
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;
namespace WebApplication1 { public partial class WebForm7 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DropDownList1.Items.Add(new ListItem("Select Item", "")); DropDownList1.Items.Add(new ListItem("Item 1", "1")); DropDownList1.Items.Add(new ListItem("Item 2", "2")); DropDownList1.Items.Add(new ListItem("Item 3", "3")); DropDownList1.Items.Add(new ListItem("Item 4", "4")); DropDownList1.Items.Add(new ListItem("Item 5", "5")); } }
protected void Unnamed1_Click(object sender, EventArgs e) { Label1.Text = "Page post take place."; } } }
Now run the application for checking output.
|
No comments:
Post a Comment