Professional Documents
Culture Documents
postbacks.)
• Properties
– Name ---- Gets or sets the name of a cookie.
– Value ---- Gets or sets an individual cookie value.
– Expires ---- Gets or sets the expiration date and time for the
cookie.
HttpCookieCollection
• The HttpCookieCollection class contains cookies that were
pairs.
h1.Expires=DateTime.Now.AddDays(-2); //deletes
HttpCookieCollection Example
using System; using System.Web;
public partial class _Default : System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e) { }
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie h1 = new HttpCookie("Username", "vista");
HttpCookie h2 = new HttpCookie("Password", "symphony");
h1.Expires=DateTime.Now.AddDays(2);
h2.Expires=DateTime.Now.AddDays(2);
HttpCookieCollection c = Response.Cookies; c.Add(h1); c.Add(h2);
}
protected void Button2_Click(object sender, EventArgs e) {
HttpCookieCollection c = Request.Cookies;
string[] s = c.AllKeys;
for (int i = 0; i < s.Length; i++)
{
HttpCookie hc = Request.Cookies[i];
Label1.Text += "<br/>" + hc.Name +":"+ hc.Value + "<br/>";
}
} }
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<html ><head runat="server"><title></title></head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label2" runat="server" Text="Enter User Name"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox> <br />
<asp:Label ID="Label3" runat="server" Text="Enter Password"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password" >
</asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click“ Text="Set" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click“ Text="Get" />
<br /> <asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form> </body>
</html>
Default.aspx.cs
using System;
using System.Web;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Cookies["uname"].Value = TextBox1.Text;
Response.Cookies["pwd"].Value = TextBox2.Text;
Response.Cookies["uname"].Expires = DateTime.Now.AddDays(2);
Response.Cookies["pwd"].Expires = DateTime.Now.AddDays(2);
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = "<br/>" + Request.Cookies["uname"].Value + ":" +
Request.Cookies["pwd"].Value+ "<br/>";
}
}
Advantages and Drawback of Cookies
• Advantages
– Cookies are very easy to create and manipulate.
– Cookies do not require any server resources since
they are stored on the client.
• Drawbacks
– Users can edit or delete cookie files.
– Cookies stores data in text files and hence not
reliable in storing sensitive data.
– Can store only small amount of data.
Session State
• HTTP is a stateless protocol.
Example:-
Session["FirstName"] = t1.Text;
Session["LastName"] = t2.Text;
Example:-
string s1 = TextBox1.Text;
Session.Add(“itm1”, s1);
Retrieving Session Variables
• String variable=Session[“session_variable_name”].ToString();
Example:-
string x = Session["itm1"].ToString();
Application State
Application[“name variable”]=“value”;
Example
using System; using System.Collections.Generic;
using System.Linq; using System.Web;
using System.Web.UI; using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
int count=0;
protected void Page_Load(object sender, EventArgs e) {
count = Convert.ToInt32(Application["counter"]);
count++;
Label1.Text = "<b>This site is visited " + count+ " times </b> ";
}
protected void Page_PreRender(object sender, EventArgs e)
{
Application["counter"] = count;
}
}
URL Encoding
• Another way of maintaining state information is by using URL encoding.
Default2.aspx?Name=Raj
• Following is an example of a URL with three Query Strings.
Default2.aspx?Name=Raj&address=konat place&city=delhi
Retrieving the value of a Query String
• To retrieve the value of a Query String attribute, use the
QueryString property of the HttpRequest object and specify
the attribute name.
• Request.QueryString["Attribute_Name"];
Or
.
.
.
Introduction to Cascading Style Sheets
• Stylesheets are used to control how elements are presented in
the Web page.
• A style sheet consists of a list of rules that affect the appearance
of one or more documents.
• It gives a uniform look and feel for your web application.
• Style sheets make it easy to specify the color and font of text,
the margin between various elements on a webpage, and a host
of other details.
Need of CSS
• It helps you to separate the style information from the content on
your web page.
• This approach has the following advantages:
– It avoids redundancy.
– It ensures easy maintenance.
The look and feel of the web site can be changed simply by
altering CSS file; in contrast use of HTML formatting
elements would require every file to be updated to change the
appearance.
• Style sheets are useful because you can use the same style sheet
for many documents.
Basic Structure of a Style
• A style sheet consists of a set of rules.
• Each rule has the following syntax.
Syntax:-
selector { property:value; }
Example:-
P { font-family: "ARIAL“; }
Note:-
The property is followed by a colon (:) and the value is
followed by a semicolon(;)
CSS Comment
• A comment line can be included in a stylesheet as follows.
/* Comment here */
Types of Selectors
Following are the common type of selectors.
• HTML Tag Selectors
• Class Selector
• ID Selector
• Grouped Selector
• Context Selector
HTML Tag Selectors
• It is used to set the style of a specific tag.
Syntax:-
selector { property:value }
• Example:-
body { font-family: ARIAL; font-size: 1em; color: aqua; }
h3 { color:aqua; }
Class Selectors
• It styles all elements with the specified class.
• It starts with a period(.) symbol.
Syntax:-
.classname { property:value; }
• Example:-
.test { font-family: ARIAL; font-size: 1em; color: aqua; }
• A class can be applied to selected HTML elements as follows.
h1.test{ color:silver;font-size:2em; }
Here the style ‘test’ is applicable only to h1 tag.
so <p class= "test">STYLE NOT APPLIED</p>
<H1 class="test"> STYLE APPLIED </H1>
ID Selectors
• The id selector is used to specify a style for a single, unique
element.
• It is defined with a “#” symbol at the beginning.
Syntax:-
#selector { property:value; }
• Example:-
#abc { color:navy; font-family: "ARIAL; font-size: 1em; }
Grouped Selectors
• If there are multiple elements with the same style then these
elements can be grouped and a common style definition can be
made for the group.
• Separate each element in the group with a comma.
• Example:-
h1,h2,h3, p
{
color:lime;
}
This is equivalent to:
h1{color:lime}
h2{color:lime}
h3{color:lime}
b {color:lime}
Grouped Selectors
• If there are multiple elements with the same style then these
elements can be grouped and a common style definition can be
made for the group.
• Separate each element in the group with a comma.
• Example:1-
h1,h2,h3, p h1{color:lime}
{ h2{color:lime}
color:lime; This is equivalent
h3{color:lime}
} to:
b {color:lime}
• Example:2-
h2,P.TES
{
color:teal;font-size:2em
}
Context Selectors
• These are selectors which work only in certain contexts.
• Syntax:
Tag1 Tag2 { ... }
Here the style will be applied to the Tag2 only when it is within
the Tag1, as it is shown below.
<Tag1>
<Tag2> ... </Tag2>
</Tag1>
• Example:-
PB {
font-weight: bold; color:Navy
}
• The above style is applied only when <p> and <b> tags come
together as: <p><b>Context Selectors</b></p>
Type of CSS
Following are the types of CSS:
• Inline Styles
• Internal (Embedded) Styles
• External styles
Inline Styles
• Inline Styles are placed directly inside the HTML element in the
code.
• Style applies to the tag in which it is defined.
• This kind of styles are useful when you want apply style to an
individual element in an HTML document.
• Style is defined within an HTML element and hence can be
reused.
<body>
<h1 style=“Style definition goes here”>Applied Only to this text </h1>
</body>
Inline Style Example
<html>
<body style=" background:silver; color:Navy;font-size:2em" >
<h1 style="color:Olive" >Inline Style Sheet Example</h1>
</body>
</html>
Internal (Embedded) Styles
• Style declaration is in the head of the page.
• It can be used only for the web page in which it is defined.
• So embedded styles are not reusable for other pages.
<head>
<style type="text/css">
Style definition goes here
</style>
</head>
<body>
</body>
Internal Style Example
<html>
<head>
<style type="text/css">
body { background:silver}
h1,p { color:Navy;font-size:2em }
</style>
</head>
<body>
<h1><u>Internal Style Sheet Example</h1></u>
<p>Style declaration is in the head of the page.</p>
<p>It can be used only for the web page in which it is defined.</p>
<p>So embedded styles are not reusable for other pages.</p>
</body>
</html>
External Style Sheets
• External style sheets consists of a list of rules that can be applied
to one or more web pages.
• It helps you to separate the style information from the HTML file.
• The styles are external to, or outside of, the web page.
• Predefined names:
White, black, red …
• 8-bit hexadecimal intensities for red, green, blue:
#ff0000
R G B
• 0-255 decimal intensities:
rgb(255,255,0)
R G B
• Percentage intensities:
rgb(80%,80%,100%)
R G B
Background Properties
Property Description
Background-color Defines background color. The color value can be hexadecimal
or any valid color constant.
background-color:#ffffff
background-color:ff0000
background-color:red
background-color:rgb(255,255,0)
Background-image Values: none or url
body{background-image:url("logo.jpg"}
body{background-image:url("http://www.tineye.com
/images/widgets/mona.jpg")}
Background-repeat Values: Repeat, repeat-x, repeat-y, no-repeat
Background-attachment Values: Scroll, fixed
Border Properties
Property Description
Setting unique The directions are top, bottom, left and right. Using this
Border to each property one can set unique border for each side of an
direction html element.
Border-bottom-style:double
Border-top-style:single
Border-left-style:none
Border-right-style:none
Margin Properties
Property Description
Margin Specifies the margin of the html element.
The value can be in px, cm,% etc.
Eg:margin:2Px means a uniform margin of 2Px on all sides.
Setting unique The directions are top, bottom, left and right. Using this property
margin to each one can set unique border for each side of an html element.
direction margin-bottom:2Px
margin-top:3Px
margin-left:4Px
margin-right:5Px
Span
• It spans the content into sections.
• Each section can then have its own formatting, as specified by
the CSS.
• It is an inline container.
• No line break is created when a span is declared.
• In the following example all pizza restaurants names are
classed and styled.
• .pizza{font-size:24;font-weight:bold;color:#00ff00;text-
transform:uppercase;}
• International brans of Pizza include: <span class="pizza">
Domino's Pizza</span>,<span class="pizza">Pizza
Express</span>,<span class="pizza"> Pizza Hut</span>, etc.
• Output:
International brans of Pizza include: DOMINO'S
PIZZA,PIZZA EXPRESS,PIZZA HUT, ETC.
Div
• It spans the content into sections.
• Each section can then have its own formatting, as specified by the CSS.
• It is a block level container.
• There is a line break after div tag.
• In the following example all pizza restaurants names are classed and styled.
• .pizza{font-size:24;font-weight:bold;color:#00ff00;text-
transform:uppercase;}
• International brans of Pizza include: <div class="pizza"> Domino's
Pizza</div>,<div class="pizza">Pizza Express</div>,<div class="pizza">
Pizza Hut</div>, etc.
• OUTPUT
International brans of Pizza include:
DOMINO'S PIZZA
,PIZZA EXPRESS
,PIZZA HUT
,ETC.
StyleSheets in Asp.Net
(Visual Studio 2010)
Steps to Add a StyleSheet in Project
• Select the project path from Solution Explorer.
• Right click and select the Add New Item from the Popup Menu.
• Select StyleSheet from the list of items displayed in the Add Item Dialog
Box.
• Click Add Button.
• Now Open the StyleSheet file and Add relevant style definitions.
StyleSheet Example
.myclass
{
border:3px solid Green;
width: 200px;
height: 50px;
background-color:Aqua;
color: #FF00FF;
font-family:Times New Roman;
font-size:large;
font-weight: bold;
}
p
{
border:2px solid red;
width:100%;
Height:100px;
background-color:Olive;
color:White;
font-size:medium;
font-weight: bold;
}
Code to link a StyleSheet with a WebPage
• You can use a style sheet on the specific page where you want it by adding
reference like below.
<head runat="server">
</head>
• Place the <link> tag within the <Head> </Head> tag of the
respective page.
Master Pages
• Master page provides a framework (common content as well as the
layout ) within which the content from other pages can be displayed.
• So the Content Pages need not have to duplicate code for shared
elements within your Web site.
• It gives a consistent look and feel for all pages in your application.
Master Pages
Master Pages
Master Pages
• The master page layout consist of regions where the content from
each content page should be displayed.
• These are the regions where you are going to have dynamic content
in your page.
<html>
<head runat="server"> <title></title>
</div></form></body>
</html>
What is the difference between a regular ASP Page and a Content Page?
Or
How to convert a regular ASP Page and a Content Page?
• Example:-
AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
Master Page Demo
MasterPage.master
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs"
Inherits="MasterPage" %>
<html > <head id="Head1" runat="server"> <title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head><body> <form id="form1" runat="server">
<center><table style="width:100%">
<tr><td colspan="2"> <center><h1 style="background: #C4376B;">
ASP.NET TUTOR</h1></center></td></tr>
<tr><td style=" background:#100000; width:400; height:1500; vertical-align:top; ">
<asp:treeview ShowLines="true" Font-Size="Large" ForeColor="White" ID="Treeview1"
runat="server" DataSourceID="SiteMapDataSource1"></asp:treeview>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
</td><td style=" vertical-align:top;">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder></td></tr> </table></center>
</form></body></html>
Web.sitemap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="Default.aspx" title="ASP.NET Master Pages"
description="">
</siteMap>
Default.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<h1>Master Pages</h1>
<ul style=" color:Maroon; line-height:60px; font-size:xx-large;">
<li>The master page layout consist of regions that each derived page can customize.</li>
<li>These regions can be set using ContentPlaceHolder server controls.</li>
<li>These are the regions where you are going to have dynamic content in your page.</li>
<li>A derived page also known as a content page is simply a collection of blocks the runtime
will use to fill the regions in the master.</li>
</ul>
</asp:Content>
Default2.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master"
AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>