0% found this document useful (0 votes)
23 views6 pages

Visitors and Validation

The document discusses adding a visitor counter to an ASP.NET web page using session variables to track the number of visitors. It also covers adding validation controls like required fields and regular expressions to a registration form.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

Visitors and Validation

The document discusses adding a visitor counter to an ASP.NET web page using session variables to track the number of visitors. It also covers adding validation controls like required fields and regular expressions to a registration form.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Note : If the Code is for a web page

then make sure you do not mess with


the first two lines of your own aspx
file( HTML to HTML only! ) and also
do not change namespace and class
name in your own c# file!

No. Of Visitors and Validations

No. Of Visitors

Code :

Aspx File

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"


Inherits="YourNamespace.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Visitor Counter</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Visitor Counter</h1>

<!-- Display the number of visitors -->


<p>Number of Visitors: <asp:Label ID="lblVisitorCount" runat="server"></asp:Label></p>
</div>
</form>
</body>
</html>

C# File

using System;

namespace YourNamespace
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Increment the visitor count and display it
IncrementVisitorCount();
DisplayVisitorCount();
}
}

private void IncrementVisitorCount()


{
// Check if the session variable exists
if (Session["VisitorCount"] == null)
{
// If not, initialize it with 1
Session["VisitorCount"] = 1;
}
else
{
// If exists, increment it by 1
Session["VisitorCount"] = (int)Session["VisitorCount"] + 1;
}
}

private void DisplayVisitorCount()


{
// Display the visitor count in the label
lblVisitorCount.Text = Session["VisitorCount"].ToString();
}
}
}

Validations
- first update the web.config file with adding the following tag init or
else you will get error!, add this inside the configuration tag
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>

- there are two files for it


* First is the registration from
* Second to display the data (ResultPage)

Registration aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Validations.aspx.cs"
Inherits="PracticalTest1.Validations" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Registration Form</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Registration Form</h1>

<!-- Name -->


<asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>
<asp:TextBox ID="txtName" runat="server" MaxLength="25"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" runat="server" ControlToValidate="txtName"
ErrorMessage="Name is required." Display="Dynamic"
ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revName" runat="server" ControlToValidate="txtName"
ErrorMessage="Invalid characters. Only alphabets are allowed." ValidationExpression="^[a-
zA-Z\s]+$"
Display="Dynamic" ForeColor="Red"></asp:RegularExpressionValidator>

<br />

<!-- Age -->


<asp:Label ID="lblAge" runat="server" Text="Age:"></asp:Label>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAge" runat="server" ControlToValidate="txtAge"
ErrorMessage="Age is required." Display="Dynamic"
ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="rvAge" runat="server" ControlToValidate="txtAge" Type="Integer"
MinimumValue="18" MaximumValue="32" ErrorMessage="Age should be between 18 and
32." Display="Dynamic"
ForeColor="Red"></asp:RangeValidator>

<br />

<!-- Address -->


<asp:Label ID="lblAddress" runat="server" Text="Address:"></asp:Label>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAddress" runat="server" ControlToValidate="txtAddress"
ErrorMessage="Address is required." Display="Dynamic"
ForeColor="Red"></asp:RequiredFieldValidator>

<br />

<!-- Mobile Number -->


<asp:Label ID="lblMobile" runat="server" Text="Mobile Number:"></asp:Label>
<asp:TextBox ID="txtMobile" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvMobile" runat="server" ControlToValidate="txtMobile"
ErrorMessage="Mobile Number is required." Display="Dynamic"
ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revMobile" runat="server"
ControlToValidate="txtMobile"
ErrorMessage="Invalid Mobile Number." ValidationExpression="^\d{10}$"
Display="Dynamic"
ForeColor="Red"></asp:RegularExpressionValidator>

<br />

<!-- Email -->


<asp:Label ID="lblEmail" runat="server" Text="Email:"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" ControlToValidate="txtEmail"
ErrorMessage="Email is required." Display="Dynamic"
ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revEmail" runat="server" ControlToValidate="txtEmail"
ErrorMessage="Invalid Email address." ValidationExpression="\b[A-Za-z0-9._%+-]+@[A-Za-
z0-9.-]+\.[A-Z|a-z]{2,}\b"
Display="Dynamic" ForeColor="Red"></asp:RegularExpressionValidator>

<br />

<!-- Submit and Cancel Buttons -->


<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" />

</div>
</form>
</body>
</html>

Registration C#

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

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

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Display entered information on a new page
Response.Redirect($"ResultPage.aspx?
Name={txtName.Text}&Age={txtAge.Text}&Address={txtAddress.Text}&Mobile={txtMobile.Text}");
}

protected void btnCancel_Click(object sender, EventArgs e)


{
// Clear all text fields
txtName.Text = string.Empty;
txtAge.Text = string.Empty;
txtAddress.Text = string.Empty;
txtMobile.Text = string.Empty;
}
}
}

ResultPage aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ResultPage.aspx.cs"


Inherits="PracticalTest1.ResultPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Registration Result</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Registration Result</h1>

<!-- Display Entered Information -->


<asp:Label ID="lblResult" runat="server" Text=""></asp:Label>

</div>
</form>
</body>
</html>

ResultPage C#

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

namespace PracticalTest1
{
public partial class ResultPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Retrieve information from query parameters
string name = Request.QueryString["Name"];
string age = Request.QueryString["Age"];
string address = Request.QueryString["Address"];
string mobile = Request.QueryString["Mobile"];
// Display the entered information
lblResult.Text = $"Name: {name}<br/>" +
$"Age: {age}<br/>" +
$"Address: {address}<br/>" +
$"Mobile Number: {mobile}";
}
}
}
}

You might also like