Unit Ii
Unit Ii
NET UNIT 2
3. CompareValidator
4. RegularExpressionValidator
5. CustomeValidator
6. ValidationSummary
These validation controls can be apply on any control that has decorated with
ValidationProperty attribute.
RequiredFieldValidator control:
This validation control checks to required value is input or not into specified
input control before submitting the form. This control needs to link any one input
control like TextBox.
Syntax:
Example:
<div> Your
Name
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1"
ErrorMessage="Your Name is empty"
ToolTip="Input Your Name" Text="(Required)" />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
<asp:Button ID="Button2" runat="server" Text="Cancel"
CausesValidation="False" />
</div>
</form>
</body>
</html>
RangeValidator control:
This validation control checks input value falls between a certain minimum
and maximum value. This control needs to link any one input control like
TextBox.
Syntax:
Example:
Syntax:
<asp:CompareValidator ID="CompareValidator1"
runat="server" />
Important Properties:
ControlToValidate="TextBox1" Operator="DataTypeCheck"
Type="Date"
Text="(Invalid (mm/dd/yyyy)" />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
<asp:Button ID="Button runat="server" Text="Cancel"
CausesValidation="False" />
</div>
</form>
</body>
</html>
Example2: Check input integer value is more than 18.
</body>
</html>
Example3: Compare input password to confirm password.
RegularExpressionValidator control:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" />
Important Properties:
Your Email ID
<asp:TextBox ID="TextBox1" runat="server"/>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ValidationExpression=
"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Text="(Invalid
EmailID format" />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
<asp:Button ID="Button runat="server" Text="Cancel"
CausesValidation="False" />
</div>
</form>
</body>
</html>
ValidationSummary control:
This control display a list of all validation errors which are given in
ErrorMessage of each validation control.
Syntax:
Important Properties:
Your Email ID
<asp:TextBox ID="TextBox4" runat="server"/>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ControlToValidate="TextBox4"
ErrorMessage="Invalid Email ID Format" ValidationExpression=
"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Text="*" />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
<asp:Button ID="Button2" runat="server" Text="Cancel"
CausesValidation="False" />
</div>
</form>
</body>
</html>
CustomValidator control:
If none of the other validation controls perform the type of validation that we
need then we can use CustomValidator control. In this control, we can
associate function for a custom validation.
Syntax:
<asp:CustomValidator ID="CustomValidator1"
runat="server" />
Important Properties:
<html>
<head><title>Custome Validation Page</title></head>
<body>
<form id="form1" runat="server">
<div>
Input string of 10 character
<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBox1"
OnServerValidate="CustomValidator1_ServerValidate" Text="(String
Length is greater than 10) />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
</div>
</form>
</body>
</html>
Basics of Regular expression
d (for digits)
w (for character)
{n} (n is exact number of allowed digits or character) [a-z](match any
12 DEPARTMENT OF COMPUTER SCIENCE-Mrs. SUGANYA, AP/RAAK
22UBCAC62: ASP.NET UNIT 2
CALENDAR CONTROL:
The Calendar control displays a calendar that can use as a date picker or to
display a list of upcoming events.
Syntax:
Properties:
Events:
<html>
14 DEPARTMENT OF COMPUTER SCIENCE-Mrs. SUGANYA, AP/RAAK
22UBCAC62: ASP.NET UNIT 2
ADROTATOR CONTROL:
Syntax:
Properties:
Events:
<Advertisements>
<Ad>
<ImageUrl>~/AdImage/Ad1.jpg</ImageUrl>
<AlternateText>LRsir</AlternateText>
<NavigateUrl>http://www.LRsir.net</NavigateUrl>
<Impressions>50</Impressions>
<Keyword>banner</Keyword>
<Width>400</Width>
<Height>200</Height>
</Ad>
<Ad>
<ImageUrl>~/AdImage/Ad2.jpg</ImageUrl>
<AlternateText>Advance College</AlternateText>
<NavigateUrl>www.advcol.com</NavigateUrl>
<Impressions>20</Impressions>
<Keyword>banner</Keyword>
<Width>400</Width>
<Height>200</Height>
</Ad>
</Advertisements>
6) Attach adXML.xml file to AdvertisementFile property and add
Keyword value to KeywordFilter property.
<asp:AdRotator ID="AdRotator1" runat="server"
16 DEPARTMENT OF COMPUTER SCIENCE-Mrs. SUGANYA, AP/RAAK
22UBCAC62: ASP.NET UNIT 2
This control is used to create vertical and horizontal list of link in drop down
menu format.
Syntax:
<asp:Menu ID="Menu1" runat="server"/>
Properties:
if (ViewState["count"] == null)
{
ViewState["count"] = "1";
}
else
{
int i = Convert.ToInt32(ViewState["count"]) + 1; ViewState["count"]
= i.ToString();
}
}
}
</script>
<html>
<head><title>ViewState Page</title></head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="ok"
OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Session:
It is a server side state management technique i.e. as long as the user is on
current page or next page, state is always available and when user idles up
to specified session time then session state lost. The server maintains the
state of user information by using a session ID.
When user makes a request without a session ID, ASP.NET creates a
session ID and sends it with every request and response to the same user.
Syntax: Session["Variable_Name"]
Session Events: We can executes codes when session starts or end using
Session_Start and Session_End events. Session events are defining in
Global.asax file that creates into root folder of website.
1. Global.aspx
4. nextpage.aspx
21 DEPARTMENT OF COMPUTER SCIENCE-Mrs. SUGANYA, AP/RAAK
22UBCAC62: ASP.NET UNIT 2
<html>
<head><title>Next Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" />
</div>
</form>
</body>
</html>
APPLICATION:
Application state is a server side state management technique. The data stored
in application state is common for all users of that particular ASP.NET
application and can be accessed anywhere in the application. It is also called
application level state management. Data stored in the application should be
of small size.
Syntax: Application["Variable_Name"]
Application Events:
Application events are defining in Global.asax file that creates into root folder
of website.
Application_Start: It is raised when the first request is made using
domain(www.LRsir.net) of web site.
Application_End: It is raised just before the domain ends, server restart, when the
first request is made using domain(www.LRsir.net) of web site.
1. Global.aspx
<body>
<form id="form1" runat="server">
<div>
Total clicks by all users
<asp:Label ID="Label1" runat="server"/>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Submit" />
</div>
</form>
</body>
</html>
In ASP.NET, managing state between requests and passing data between different
parts of the application is a crucial part of web development. Various techniques are
available for storing data on the client and server side. Here’s a breakdown of Control
State, Hidden Fields, Cookies, and Query Strings in ASP.NET:
1. Control State
Control state in ASP.NET is used to store data specific to a control that must persist
across postbacks, similar to the ViewState, but it is more reliable in certain scenarios
because it is not turned off, even if the EnableViewState property of a control is set to
false.
Use Cases: Storing data that is critical to the control’s operation (e.g., a custom
control).
Key Characteristics:
o It persists across postbacks.
o It is stored in the page’s hidden section like ViewState, but it cannot be
disabled.
o Data in the control state is not accessible by the user unless they have
access to the page’s source code.
How to use:
You can store data in the control state by overriding the SaveControlState() and
LoadControlState() methods of a custom control.
csharp
Copy code
public class MyCustomControl : WebControl
{
private string myControlData;
24 DEPARTMENT OF COMPUTER SCIENCE-Mrs. SUGANYA, AP/RAAK
22UBCAC62: ASP.NET UNIT 2
o They are stored on the client-side, meaning users can modify or delete
them.
o Cookies can be persistent (stay across sessions) or session cookies (only
for the duration of the browser session).
How to use: Creating and retrieving cookies in ASP.NET:
csharp
Copy code
// Creating a cookie
HttpCookie cookie = new HttpCookie("UserPreferences");
cookie["Theme"] = "Dark";
cookie.Expires = DateTime.Now.AddDays(7); // Persist for 7 days
Response.Cookies.Add(cookie);
// Retrieving a cookie
HttpCookie retrievedCookie = Request.Cookies["UserPreferences"];
if (retrievedCookie != null)
{
string theme = retrievedCookie["Theme"];
}
4. Query Strings
Query strings are parts of the URL that can be used to pass information between
pages. They are visible to the user and can be modified in the browser's address bar.
Use Cases: Passing data in the URL, such as page number in a paginated list,
search terms, or user identifiers.
Key Characteristics:
o Data is visible to the user.
o It has a length limitation (varies by browser, typically around 2048
characters).
o It is often used for GET requests, where the parameters are appended to the
URL.