Menu ▼



Posted By :
avatarmrkraju
Total Views : 868

Asp.net requiredfieldvalidator examlpe

Asp.net requiredfieldvalidator is used to make an input control as required field(mandatory). If you want to associate the requiredfieldvalidator to a textbox then you have to give the textbox id to requiredfieldvalidator ControlToValidate property.

Syntax

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Name Required" ForeColor="Red" ControlToValidate="txtName"></asp:RequiredFieldValidator>

Example

ASPX Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
        Name:<br />
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Name Required"
            ForeColor="Red" ControlToValidate="txtName"></asp:RequiredFieldValidator>
        <br />
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
        <br />
        <br />
        <asp:Label ID="lblMessage" runat="server" Style="color: Green; font-size: 18px; font-weight: bold;"></asp:Label>
    </div>
    </form>
</body>
</html>

ASPX Page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Globalization;

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

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "Submitted successfully.";
    }
}






comments powered by Disqus


Search dotnetlearners.com