RequiredFieldValidator Example in ASP.NET using C#

RequiredFieldValidator Example in ASP.NET using C#


Posted in : ASP.Net Posted on : November 30, 2010 at 5:48 PM Comments : [ 0 ]

In this article we will introduce with RequiredFieldValidator control in ASP.NET using C#.

RequiredFieldValidator Example in ASP.NET using C#

RequiredFieldValidator control in ASP.NET is a simple validator which checks to see if the data is entered for the attached control. You can set the ErrorMessage property which you want to display. The another property is ControlToValidate in which you can set the attached control instance.

RequiredFieldValidator.aspx (Design Page):

RequiredFieldValidator.aspx (source code):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" AutoEventWireup="true"
CodeFile="RequiredFieldValidator.aspx.cs" Inherits="RequiredFieldValidator" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<h2 style="color: Green">
RequiredFieldValidator in ASP.NET 4 , C#</h2>
<br />
<table cellpadding="0" cellspacing="0" style="height: 177px; width: 246px">
<tr>
<td><strong>User Name:</strong></td>
<td><asp:TextBox ID="userTextBox" runat="server" Font-Names="Verdana" /></td>
</tr>
<tr>
<td colspan="2"><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please enter user name." ControlToValidate="userTextBox" ForeColor="Red" ValidationGroup="Submit">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<strong>Password: </strong>
</td>
<td>
<asp:TextBox ID="passwordTextBox" runat="server" 
TextMode="Password" Font-Names="Verdana" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
ErrorMessage="Please enter password."
ControlToValidate="passwordTextBox" ForeColor="Red" ValidationGroup="Submit">
</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><asp:Button ID="submitButton" runat="server" Text="Submit" Width="80px" 
onclick="submitButton_Click" ValidationGroup="Submit" /></td>
</tr>
<tr>
<td colspan="2"><asp:Label ID="Label1" runat="server" Font-Bold="True" 
ForeColor="#000099"></asp:Label>&nbsp;</td>
</tr>
<tr>
<td colspan="2"><asp:Label ID="Label2" runat="server" Font-Bold="True" 
ForeColor="#000099"></asp:Label></td>
</tr>
</table>
</div>
</asp:Content>

RequiredFieldValidator.aspx.cs (C# code file):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class RequiredFieldValidator : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submitButton_Click(object sender, EventArgs e)
{
Label1.Text = "User Name : " + userTextBox.Text;
Label2.Text = "Password : " + passwordTextBox.Text;
userTextBox.Text = "";
passwordTextBox.Text = "";
}
}

Output:


Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics