Unit 3 C#
Unit 3 C#
1. Compare Validator
This control compares the value of one input control to another. It can validate
passwords, confirm email addresses, and other scenarios where two values must
match. We can use comparison operators like: less than, equal to, greater than etc.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form runat="server">
<table>
<tr>
<td> First value</td>
<td><asp:TextBox ID="firstval" runat="server" required="true"></asp:TextBox> </td>
</tr>
<tr>
<td>Second value</td>
<td><asp:TextBox ID="secondval" runat="server"></asp:TextBox>
It should be greater than first value</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="Button1" runat="server" Text="save"/>
</td>
</tr>
</table>
< asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="Enter valid value" ForeColor="Red"
Operator="LessThan" Type="Integer"></asp:CompareValidator>
</form>
</body>
</html>
2.RangeValidator
This validator evaluates the value of an input control to check that the value lies
between specified ranges.It allows us to check whether the user input is between
a specified upper and lower boundary. This range can be numbers, alphabetic
characters and dates.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form runat="server">
<div>
<p>Enter value between 100 and 200<br/> </p>
<table>
<tr>
<td><asp:Label ID="Label2" runat="server" Text="Enter a value"></asp:Label></td>
<td>
<asp:TextBox ID="uesrInput"runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ErrorMessage="Enter value in specified range"MaximumValue="199" MinimumValue="101:”></
asp:RangeValidator>
</td>
</tr>
<tr>
<td> </td>
<td><asp:Button ID="Button2" runat="server" Text="Save"/> </td>
</tr>
</table>
</div>
</form>
</body>
</html>
3. RegularExpressionValidator
This control checks if a value matches a specified regular expression pattern. For
example, it can validate email addresses, phone numbers, zip codes, and other
input types
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form runat="server">
<div>
<table class="auto-style1">
<tr>
<td>Email ID</td>
<td>
<asp:TextBox ID="username" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"ControlToVa
lidate="username"
ErrorMessage="Please enter valid email" ForeColor="Red"ValidationExpression="\w+([-+.']\
w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="Button1" runat="server" Text="Save"/></td>
</tr>
</table>
</div>
</form>
</body>
</html>
4.RequiredFieldValidator
This control ensures that a field is not left empty or blank. It can be
used for textboxes, dropdown lists, checkboxes, and other input
controls.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form runat="server">
<table>
<tr>
<td>User Name</td>
<td>
<asp:TextBox ID="username" runat="server"></asp:TextBox>
<asp:RequiredFieldValidatorID="user" runat="server"
ErrorMessage="Please enter a user name" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Password</td>
<td>
<asp:TextBox ID="password" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="pass" runat="server" ErrorMessage="Please enter a password
"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Button runat="server" Text="login"/>
</td>
</tr>
</table>
</form>
5.ValidationSummary
This validator is used to display list of all validation errors in the web form.
We can set DisplayMode property to display error messages as a list, bullet list
or single paragraph.
<html>
<body>
<form id="form1" runat="server">
<table>
<tr><td>User Name</td>
<td>
<asp:TextBox ID="username" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="user" runat="server"
ErrorMessage="Please enter a user name">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2">Password</td>
<td>
<asp:TextBox ID="password" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="pass" runat="server"
ErrorMessage="Please enter a password" >*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td><asp:Button ID="Button1" runat="server"Text="login"/></td>
<td>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"/>
</td>
</tr>
</table>
</form>
</html>
Navigation Control ASP .Net
Navigation controls are basically used to navigate the user through webpage .
It is more helpful for making the navigation of pages easier .
The navigation menus are based on the site map.
1. Dynamic Menu
It was the very difficult task to maintain the menu of a large website and
time consuming. It is used to display the Menus. You can use it as easy as
other Navigation controls. Menu can be stored in a file to make it easier to
maintain. This file is normally called web. Sitemap, and is stored in the root
directory of the web.
<!DOCTYPE>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Menu ID="Menu1" runat="server">
<DynamicItemTemplate>
Audio songs
<br />
Video songs
<br />
</DynamicItemTemplate>
<StaticItemTemplate>
Songs
<br />
</StaticItemTemplate>
<Items>
<asp:MenuItem Text="Songs" Value="Songs">
<asp:MenuItem Text="New Item" Value="New Item"></asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>
</div></form>
2.Tree Views
A Tree View control displays a hierarchical list of items using lines to connect
related items in a hierarchy. Each item consists of a label and an optional
bitmap. Windows Explorer uses a Tree View control to display directories. You
can use the Tree View control in any situation in which you need to display
hierarchical data.
<body>
<form id="form1" runat="server">
<div>
<table bgcolor="#ffccff">
<tr>
<td>
<asp:TreeView ID="TreeView1" runat="server">
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True"/>
<SelectedNodeStyle Font-Underline="True"/>
<Nodes>
<asp:TreeNode Text="Employee" Value="Employee">
<asp:TreeNode Text="Emp_Name" Value="Emp_Name">
<asp:TreeNode Text="First Name" Value="First Name"></asp:TreeNode>
<asp:TreeNode Text="Last Name" Value="Last Name"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="Emp_Address" Value="Emp_Address">
<asp:TreeNode Text="Local" Value="Local"></asp:TreeNode>
<asp:TreeNode Text="Permanent" Value="Permanent"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="Emp_Contact no." Value="Emp_Contact no.">
<asp:TreeNode Text="Home" Value="Home"></asp:TreeNode>
<asp:TreeNode Text="Office" Value="Office"></asp:TreeNode>
</asp:TreeNode>
</Nodes>
<NodeStyle Font-Names="Verdana"/>
</asp:TreeView>
</td>
</tr>
</table>
<!DOCTYPE>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SiteMapPath ID="SiteMapPath1" runat="server" >
<PathSeparatorStyle Font-Bold="True" ForeColor="#990000" />
<CurrentNodeStyle ForeColor="#333333" />
<NodeStyle Font-Bold="True" ForeColor="#990000" />
<RootNodeStyle Font-Bold="True" ForeColor="#FF8000" />
</asp:SiteMapPath>
</div>
</form>
</body>
</html>
Web Control Event
An event is a message sent by an object to signal the occurrence of an action.
This action can be caused by user interaction such as button click, mouse click, etc.
The Object that sent the event is called the event sender.
The object that receives the event and responds according to that is called the event
receiver.
Event Arguments
ASP.NET event handlers generally take two parameters and return void. The first
parameter represents the object raising the event and the second parameter is event
argument.
The general syntax of an event is:
private void EventName (object sender, EventArgs e);
<table>
<tr>
<td>First value</td>
<td><asp:TextBox ID="firstvalue" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Second value</td>
<td><asp:TextBox ID="secondvalue" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Sum</td>
<td><asp:TextBox ID="total" runat="server"></asp:TextBox> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td><asp:Button ID="Button1" runat="server" OnClick="Button1_Click"Text="Sum">
</tr>
</table>
namespace asp.netexample
{
public partial class EnventHandling : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(firstvalue.Text);
int b = Convert.ToInt32(secondvalue.Text);
total.Text = (a + b).ToString();
}
}
}
In addition to static text and controls that will appear on all pages, the master
page also includes one or more ContentPlaceHolder controls.
<html>
<head runat="server" >
<title>Master page title</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td><asp:contentplaceholder id="Main" runat="server" /></td>
<td><asp:contentplaceholder id="Footer" runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>
Content Page Example
<%@ Page MasterPageFile="master1.master" %>
<asp:Content ContentPlaceHolderId="CPH1" runat="server">
<h2>Individual Content</h2>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</asp:Content>
The content page above is one of the individual content pages of the web.
It is a collection of files that define the looks of a page. It can include skin files, CSS
ASP.NET themes are a collection of properties that define the appearance of pages and
controls in your Web site.
A theme can include skin files, which define property settings for ASP.NET Web server
controls, and can also include cascading style sheet files (.css files) and graphics
If you are using a template, copy all the files of your template to the template
folder.
If you are not using a tempate, create a CSS file theme1.css in the Theme1
body
{
background-color: black;
color: white;
}
To apply a theme to a Web site
1. In the application's Web.config file, set the <pages> element to the name of the
theme, either a global theme or a page theme, as shown in the following
example:
<configuration>
<system.web>
<pages theme="ThemeName" />
</system.web>
</configuration>
<configuration>
<system.web>
<pages styleSheetTheme="Themename" />
</system.web>
</configuration>
Set the Theme or StyleSheetTheme attribute of the @ Page directive to the name
of the theme to use, as shown in the following example:
1.DataList
The ASP.NET DataList control is a light weight server side control that works as a
container for data items.
It displays data from the data source. The data source can be either a DataTable
or a table from database.
<html>
<body>
<form id="Form2" runat="server">
<asp:DataList id="DataList1" runat="server">
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
...
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>
2. DetailsView
3.FormView
This control displays a single record of data at a time like DetailsView control and
supports the editing of record.
This control requires the use of template to define the rendering of each item.
Developer can completely customize the appearance of the record.
4.GridView
This control displays the data in tabular form.
It not only support the editing and deleting of data, it also support sorting and
paging of data.
Default behavior of this control is display the data without Auto generate columns. This will give
flexible result to display table structure of data.
5.ListView
This will give all format of data like GridView , DataList, Repeater and Form view type of data.
This is the richest control to display table structure of data. The ListView control displays
columns and rows of data and allows sorting and paging.
6.Repeater
This control is used to display the repeated list of the items that are bound to
the control.
It provides complete flexibility in regards to the HTML presentation.
The Repeater control may be bound to a database table, an XML file, or another list of
items.
Code Behind File
Code Behind refers to the code for an ASP.NET Web page that is written in a
separate class file that can have the extension of .aspx.cs or .aspx.vb.
Here the code is compiled into a separate class from which the .aspx file derives.
You can write the code in a separate .cs or .vb code file for each .aspx page.
One major point of Code-Behind is that the code for all the Web pages is compiled
into a DLL file that allows the web pages to be hosted free from any Inline Server
Code.
The code-behind class file is compiled so that it can be created and used as an
object. This allows access to its properties, its methods, and its event handlers.
The primary goal of the code-behind file is to enable you to write more
modular code so that you can keep the code and layout physically
separate.
you store directives and the page layout in one .ASPX file, and the code
for them in a separate file written in Microsoft Visual Basic or
Visual C#. The link to the code-behind file is defined by using an
attribute of the @ Page directive.
When users point their browsers to the .ASPX page, the code-behind
class file runs and dynamically produces the output for the page. When
the page is requested, the code-behind file is loaded and treated as an
integral part of the page.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
}
ASP.NET Page Life Cycle includes events PreInit, Init, InitComplete, OnPreLoad, Load,
PostBack, LoadComplete, OnPreRender, OnSaveStateComplete, Render, and UnLoad.
5) Application End – This is the final part of the application. In this part,
the application is finally unloaded from memory.
Web.Config File
It is an application configuration file of the Official Microsoft ASP.NET site written in XML
format and is responsible for controlling the application behavior.
A configuration file (web.config) is used to manage various settings that define a
website.
The settings are stored in XML files that are separate from your application code.
In this way you can configure settings independently from your code.
Generally a website contains a single Web.config file stored inside the application
root directory.
USE
ASP.NET Configuration system is used to describe the properties and behaviors of
various aspects of ASP.NET applications.
Configuration files help you to manage the many settings related to your website.
Each file is an XML file (with the extension .config) that contains a set of
configuration elements. Configuration information is stored in XML-based text files.
Benefits of XML-based Configuration files
ASP.NET Configuration system is extensible and application specific information can
be stored and retrieved easily. It is human readable.
You need not restart the web server when the settings are changed in configuration
file. ASP.NET automatically detects the changes and applies them to the running
ASP.NET application.
You can use any standard text editor or XML parser to create and edit ASP.NET
configuration files.
We can create a Web.Config file using a text editor and name it Web with an
extension of .config. It must contain <configuration> element as the main
tag – all other elements are defined or configured inside it.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- configuration elements to be defined here -->
</configuration>
The first line of the code depicts the document is XML formatted with a specified version
and encoding type.
The second line of the code indicates the beginning of the <configuration> element
that will include other element inside it. It means all the configuration settings will be
defined under the main <configuration> element.
Global asax In ASP .Net
The Global.asax is also known as the ASP.NET application file and is used to serve
application-level and session-level events.
It handles important application events such as Application_Start, Application_End,
Session_Start, Session_End, and more.
It serves as the ASP.NET Application File and is typically located in the root directory of
the application.
Syantax