0% found this document useful (0 votes)
44 views25 pages

Unit 3 C#

The document discusses different ASP.NET validation controls like compare validator, range validator, regular expression validator, required field validator and validation summary. It also discusses navigation controls like dynamic menu, tree view and site map path. Finally, it briefly describes web control events in ASP.NET.

Uploaded by

chauhanjeel57
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)
44 views25 pages

Unit 3 C#

The document discusses different ASP.NET validation controls like compare validator, range validator, regular expression validator, required field validator and validation summary. It also discusses navigation controls like dynamic menu, tree view and site map path. Finally, it briefly describes web control events in ASP.NET.

Uploaded by

chauhanjeel57
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/ 25

 ASP.

NET Validation Control

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.

 It allows us to summarize the error messages at a single location.

 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>

3.Site Map Path


You can add this control to your page then view your page in browser. The Sitemap
Path control displays the navigation path of the current page. The path acts as click
able links to previous pages.The Sitemap Path control uses the web. Sitemap file by
default.
Code of Sitemap Path:
<Asp: SiteMapPath ID="SiteMapPath1" runat="server"></asp: SiteMapPath>

<!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();
}
}
}

 Web Control Method


 Web Control Properties

 Web Control Properties


 Master Page In ASP.Net
 ASP.NET master pages allow you to create a consistent layout for the pages in your
application.
 A single master page defines the look and feel and standard behavior that you want for
all of the pages (or a group of pages) in your application.
 You can then create individual content pages that contain the content you want to
display.
 When users request the content pages, they merge with the master page to produce
output that combines the layout of the master page with the content from the content
page.
 A master page is an ASP.NET file with the extension .master with a predefined layout that
can include static text, HTML elements, and server controls. The master page is identified
by a special @ Master directive that replaces the @ Page directive that is used for
ordinary .aspx pages.

 Replaceable Content Placeholders

 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.

 The @ Page directive defines it as a standard content page.

 The content page contains a content tag <asp:Content> with a


reference to the master page

 Theme ASP .Net


 A theme decides the look and feel of the website.

 It is a collection of files that define the looks of a page. It can include skin files, CSS

files & images.

 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

 Adding theme files

 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

folder (Right-click->Add->Style Sheet) and place the following code.

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>

2. To set a theme as a style sheet theme and be subordinated to local control


settings), set the styleSheetTheme attribute instead:

<configuration>
<system.web>
<pages styleSheetTheme="Themename" />
</system.web>
</configuration>

3. To apply a theme to an individual page

 Set the Theme or StyleSheetTheme attribute of the @ Page directive to the name
of the theme to use, as shown in the following example:

<%@ Page Theme="ThemeName" %>


<%@ Page StyleSheetTheme="ThemeName" %>
 Data Control
 The Controls having DataSource Property are called Data Controls in ASP.NET
 Data controls used to display the records in the form of reports.
 The data source controls are used to retrieve data from a data source, like a database or
an XML file, and then offer this data to the data-bound controls.

1.DataList

 The ASP.NET DataList control is a light weight server side control that works as a
container for data items.

 It is used to display data into a list format to the web pages.

 It displays data from the data source. The data source can be either a DataTable
or a table from database.

 DataList allows you to repeat columns horizontally or vertically.


 The DataList control renders data as a table and enables you to display data records
in various layouts, such as ordering them in columns or rows.
 You can configure the DataList control to enable users to edit or delete a record in
the table.
 We can use a DataList control where we need a single-column list.
 The DataList control works like the Repeater control, used to display the data in a
repeating structure, such as a table.
 It displays data in a format that you can define using a template and styles.
Syntax:

<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

 This control displays single record of data at a time.


 It supports updating, insertion or deletion of the record.
 It can be used in conjunction with the GridView control to provide a master-
detail view of your dat
 The GridView control in ASP.NET is used to display all the records from its data source
control in a tabular format.

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.

 <%@ Page Language="C#" Inherits="MyBasePageClass" Src="MyBasePage.cs" %>

 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;

public partial class _Default : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{

}
}

 ASP .Net Page Life Cycle

 ASP.NET Page Life Cycle includes events PreInit, Init, InitComplete, OnPreLoad, Load,
PostBack, LoadComplete, OnPreRender, OnSaveStateComplete, Render, and UnLoad.

 Page Lifecycle stages


 ASP.NET Life Cycle Events

 ASP .Net APPLICaTION Life Cycle

1) Application Start – The life cycle of an ASP.NET application starts when


a request is made by a user. This request is to the Web server for the
ASP.Net Application. This happens when the first user normally goes to the
home page for the application for the first time. During this time, there is a
method called Application_start which is executed by the web server.
Usually, in this method, all global variables are set to their default values.
2) Object creation – The next stage is the creation of the HttpContext,
HttpRequest & HttpResponse by the web server. The HttpContext is just
the container for the HttpRequest and HttpResponse objects. The
HttpRequest object contains information about the current request,
including cookies and browser information. The HttpResponse object
contains the response that is sent to the client.

3) HttpApplication creation – This object is created by the web server. It


is this object that is used to process each subsequent request sent to the
application. For example, let’s assume we have 2 web applications. One is
a shopping cart application, and the other is a news website. For each
application, we would have 2 HttpApplication objects created. Any further
requests to each website would be processed by each HttpApplication
respectively.

4) Dispose – This event is called before the application instance is


destroyed. During this time, one can use this method to manually release
any unmanaged resources.

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.

 Creating a Web.Config file

 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

<%@ Application Language="C#" %>


<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
}
</script>

You might also like