Unit 3 Asp
Unit 3 Asp
NET
What is a master page?
An HTML page comprises three elements: content, layout, and styling. For
single sourcing, it is best to separate content from layout and styling. Although CSS
files separate styling from content, layout remains embedded in the HTML code. By
using master pages, separate layout and styling from content.
A master page contains layout information and is associated with a CSS file. A
master page is a template for HTML topics. You can define the placeholders for
header, footer, and topic. You can include breadcrumbs, snippets, variables, fields,
and symbols. You can also apply condition tags to a master page.
You can create a topic using a master page or associate an existing topic with a
master page. When you create a topic using a master page, the topic placeholder
content is placed in the resulting topic. If you apply a master page to an existing
topic, the topic placeholder content is ignored. In this case, actual content of the
topic replaces the topic placeholder in the master page at the time of preview and
generation.
The layout information defined in master pages is not visible in Author view.
You can see the layout when you generate output or preview an associated topic.
The topic content is placed in the topic placeholder, and the layout is inherited from
the master page. All placeholders defined in the master page are also populated with
relevant information when you generate output.
Master Page In Asp.net
A master page provides the layout and functionality to other pages. Creating a master page
in ASP.NET is very easy. Let's start creating master page step by step.
After clicking OK button, project "masterpage" opens but no file is there (shown in the
picture),
Step 2: Add new file in to our project.
After clicking on new item, Window will open, select Web Form->Web Forms Master Page
(shown in the picture),
After clicking the add button, master page 'site1.master' adds to our project.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>c# corner</title>
<link href="css/my.css" rel="stylesheet" />
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
<title>my layout</title>
<link rel="stylesheet" type="text/css" href="my.css">
</head>
<body>
<header id="header">
<h1>c# corner</h1>
</header>
<nav id="nav">
<ul>
<li><a href="home.aspx">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Article</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<aside id="side">
<h1>news</h1>
<a href="#"><p>creating html website</p></a>
<a href="#"><p>learn css</p></a>
<a href="#">learn c#</a>
</aside>
<div id="con">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
<footer id="footer">
copyright @c# corner
</footer>
</body>
</html>
<form id="form1" runat="server">
</form>
</body>
</html>
CSS Code
#header{
color: #247BA0;
text-align: center;
font-size: 20px;
}
#nav{
background-color:#FF1654;
padding: 5px;
}
ul{
list-style-type: none;
}
li a {
color: #F1FAEE;
font-size: 30px;
column-width: 5%;
}
li
{
display: inline;
padding-left: 2px;
column-width: 20px;
}
a{
text-decoration: none;
margin-left:20px
}
li a:hover{
background-color: #F3FFBD;
color: #FF1654;
padding:1%;
}
#side{
text-align: center;
float: right;
width: 15%;
padding-bottom: 79%;
background-color: #F1FAEE;
}
#article{
background-color: #EEF5DB;
padding: 10px;
padding-bottom: 75%;
}
#footer{
background-color: #C7EFCF;
text-align:center;
padding-bottom: 5%;
font-size: 20px;
}
#con{
border:double;
border-color:burlywood;
}
Our master page is designed. Move to the next step.
Home.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireu
p="true" CodeBehind="home.aspx.cs" Inherits="masterpage.home" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="ser
ver">
<h1>Home page</h1>
</asp:Content>
Finally, our Master page is created; build and run the project.
Style sheet
Style sheets describe how documents are presented on screens, in print, or perhaps how
they are pronounced.
Cascading Style Sheets, or CSS, is the recommended way to control the presentation layer
in a web document. The main advantage of CSS over presentational HTML markup is that the
styling can be kept entirely separate from the content. For example, it's possible to store all the
presentational styles for a 10,000-page web site in a single CSS file. CSS also provides far better
control over presentation than do presentational element types in HTML.
How to make style sheet,
Step 1
Right-click on solution explorer - Add New Item - Add StyleSheet.css
Step 2
In the body class of StyleSheet.css just give the property which has to be set for any page in
the project.
Source code: StyleSheet.css
body
{
background-color :Silver ;
}
Step 3
Drag n drop StyleSheet.css from solution explorer
Step 4
Example: textbox background color has to be the same in all the pages so we define a class
.txtbox with the background-color.
Step 5
Drag a textbox and give txtbox class as CSS class in the property.
Source code: Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inher
its="_Default" %>
<!DOCTYPE html PUBLIC "-
//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server" CssClass="txtbox"></asp:TextBox>
</form>
</body>
</html>
Step 6
Add a new form and the same StyleSheet.css can be used for any textbox place in the
project.
Step 3: Select the CSS file and enter the named as Site.
Now we need to create the CSS file and Layout page. In this section we add the layout page
to our main page.
Open the Cricketers.cshtml page and add the following code at the top of the code block:
Layout = "~/_LayoutPage.cshtml";
In the code above, we merged the Layout page to the Cricketers Page. You can remove the
<!DOCTYPE>, <html>, <body> opening and closing tags because this page now uses the
layout page. I didn't remove them in my site.
Run the Site Page
Its time to check out the new page with the layout page and CSS. Launch
the Cricketers.cshtml page in the browser.
Edit the Update Page
If you added any CSS in the CreateCricketer.cshtml page then remove it and add the
following code at the top inside the code block:
Layout = "~/_LayoutPage.cshtml";
Run the page in the browser.
Now add the Page.title code in the remaining pages like DeleteCricketer.cshtml,
UpdateCricketer.cshtml and so on.
The main job in using the Wizard control is managing Wizard steps, which act as
containers for a set of controls. Each step is represented by the <asp:WizardStep> markup
tag.
In order to add or remove steps to the Wizard control, select the “Add/Remove
WizardSteps…” option from its smart tag (see Figure 4).
The “Contact Information” wizard step consists of two Labels, two TextBoxes, and the
validator controls attached to them. The TextBoxes accept the user’s email address and
telephone number. The RequireFieldValidator and RegularExpression validator controls
attached to them ensure that the end user enters the correct input. Note how this step
automatically displays Previous and Next buttons.
The following is the complete markup for your Web form:
<%@ Page Language=”C#” AutoEventWireup=”true”
CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
<html >
<head runat=”server”>
<title>Untitled Page</title>
</head>
<body>
<div>
BackColor=”#EFF3FB” BorderColor=”#B5C7DE”
OnActiveStepChanged=”Wizard1_ActiveStepChanged”
OnFinishButtonClick=”Wizard1_FinishButtonClick”>
VerticalAlign=”Top” Width=”150px”
Wrap=”False” />
<NavigationButtonStyle BackColor=”White” BorderColor=”#507CD1″
BorderStyle=”Solid”
ForeColor=”#284E98″ />
<WizardSteps>
Title=”Account Details”>
<tr>
<td nowrap=”nowrap”>
</asp:Label>
</td>
<td>
<asp:RequiredFieldValidator ID=”RequiredFieldValidator1″
runat=”server” ControlToValidate=”TextBox1″
</td>
</tr>
<tr>
<td nowrap=”nowrap”>
</asp:Label>
</td>
<td>
</asp:TextBox>
<br />
<asp:RequiredFieldValidator ID=”RequiredFieldValidator2″
runat=”server” ControlToValidate=”TextBox2″
</asp:RequiredFieldValidator>
<asp:CompareValidator ID=”CompareValidator1″
runat=”server” ControlToCompare=”TextBox2″
ControlToValidate=”TextBox3″ Display=”Dynamic”
</asp:CompareValidator>
</td>
</tr>
<tr>
<td nowrap=”nowrap”>
</asp:Label>
</td>
<td>
</asp:TextBox>
<br />
<asp:RequiredFieldValidator ID=”RequiredFieldValidator3″
runat=”server” ControlToValidate=”TextBox3″
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
</td>
</td>
</tr>
</table>
<br />
</asp:WizardStep>
Title=”Contact Information”>
<tr>
<td nowrap=”nowrap”>
</td>
<td>
<asp:RequiredFieldValidator ID=”RequiredFieldValidator4″
runat=”server” ControlToValidate=”TextBox4″
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID=”RegularExpressionValidator1″
runat=”server” ControlToValidate=”TextBox4″
ValidationExpression=”w+([-+.’]w+)*@w+([-.]w+)*.w+([-.]w+)*”>
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td nowrap=”nowrap”>
</td>
<td>
<asp:RequiredFieldValidator ID=”RequiredFieldValidator5″
runat=”server” ControlToValidate=”TextBox5″
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID=”RegularExpressionValidator2″
runat=”server” ControlToValidate=”TextBox5″
ValidationExpression=”(((d{3}) ?)|(d{3}-))?d{3}-d{4}”>
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td nowrap=”nowrap”>
</td>
<td>
</td>
</tr>
</table>
</asp:WizardStep>
<tr>
<td nowrap=”nowrap”>
Text=”User ID :”></asp:Label>
</td>
<td>
Text=”Label”></asp:Label>
</td>
</tr>
<tr>
<td nowrap=”nowrap”>
Text=”Email :”></asp:Label>
</td>
<td>
Text=”Label”></asp:Label>
</td>
</tr>
<tr>
<td nowrap=”nowrap”>
</td>
<td>
Text=”Label”></asp:Label>
</td>
</tr>
</table>
</asp:WizardStep>
</WizardSteps>
ForeColor=”White” />
BorderStyle=”Solid” BorderWidth=”2px”
HorizontalAlign=”Center” />
</asp:Wizard>
<br />
Font-Size=”Large”
ForeColor=”Red”></asp:Label> </div>
</form>
</body>
</html>
</div>
</form>
</body>
Example of MultiView Control in ASP.Net
After Design asp.net web forms take a multiview control on web forms from toolbox.
Now, add some view control in multiview control as per your needs, here we use three view control to
understand this multiview control example. The three view View1, View2 and View3, after taking views on web
page design the all view control with some web server control.
Below screen shows the final designing of web page. here we take three view control in multiview control
and design all three view control as per our requirement. There are three button on web page for display
view control on page
Here, is the out put of multiview example, when we run the application, by default we can not see any view
on web page. we just see the three button control on web page for display the view control.
When we click to button View1 then the view1 is displaying on web page.
Here we need to write below code for display view1 in button control.
When we click to button View3 then the view3 is displaying on web page.
Here we need to write below code for display view3 in button control.
protected void btnview3_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 2;
}
Here, is the code behind C# code for MultiView Control all three buttons controls.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
}
protected void btnview1_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 0;
}
protected void btnview2_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 1;
}
protected void btnview3_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 2;
}
}
<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.
void Session_Start(object sender, EventArgs e)
{
// Write code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Write code that runs when a active session is expired
}
The Session_End event is raised when session ends either because of a time out expiry or explicitly by
using Session.Abandon().
Set Session Time: Add <sessionState> into web.config file of root folder.
<configuration>
<system.web>
<sessionState timeout="10" mode="InProc" />
</system.web>
</configuration>
The Session_End event is raised only in the case of InProc mode not in the state server and SQL Server
modes.
Example: Assign name into one page and display into next page. Set 1 minute for session time.
4. nextpage.aspx
<%@ Page Language="C#" %>
<script runat="server"> void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["username"].ToString();
}
</script>
<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.
void Application_Start(object sender, EventArgs e)
{
// Write code
}
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.
void Application_End(object sender, EventArgs e)
{
// Write code
}
Application_Error: It is raised when an exception is occurs then it can be handles using this event.
void Application_Error(object sender, EventArgs e) {
// Write code
}
Example: Count total number of clicks of button by all users.
Global.aspx
void Application_Start(object sender, EventArgs e)
{
Application["count"] = 0;
}
Application.aspx
<%@ Page Language="C#" %>
<script runat="server"> void Button1_Click(object sender, EventArgs e)
{
int i=Convert.ToInt32(Application["count"]) + 1;
Application["count"] = i.ToString();
Label1.Text = i.ToString();
}
</script>
<html>
<head><title>Application State Page</title></head>
<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>
A query string is a collection of characters input to a computer or web browser. A Query String is helpful
when we want to transfer a value from one page to another. When we need to pass content between the HTML
pages or aspx Web Forms in the context of ASP.NET, a Query String is very easy to use and the Query String
follows a separating character, usually a Question Mark (?). It is basically used for identifying data appearing
after this separating symbol. A Query String Collection is used to retrieve the variable values in the HTTP query
string. If we want to transfer a large amount of data then we can't use the Request.QueryString. Query Strings are
also generated by form submission or can be used by a user typing a query into the address bar of the browsers.
Query Strings are contained in request headers. A Query String collection is a parsed version of the
QUERY_STRING variable in the Server Variables collection. It enable us to retrieve the QUERY_STRING
variable by name. When we use parameters with Request.QueryString, the server parses the parameters sent to
the request and returns the effective or specified data.
Query string is a simple way to pass some information from one page to another. The information cab be easily
pass to one page to another or to same page. With query string method the information passed in url of page
request.
This method many browsers supports only 255 character length of url in query string. The value passed will be
visible so some time it cause security issue.
For send information to other page Response.Redirect() method used and for retrieve information from url use
Request.QueryString() method used.
In Query String method we can sent value to only desired page, and value will be temporarily. So Query string
increase the overall performance of web application.
Syntax of Query String
Request.QueryString(variable)[(index).count].
Steps for perfoming string query
Step 1
Now for creating a website, click on the file, go to new and click on the website.
Step 3
Step 5
After designing the web form, we need to write the following code in the button click.
Step 6
In the second web form we take the one lable for displaying the values of the first page. For receiving the value
from the first page we write the following code on the page_load of the second page.
Step 7
Now we need to execute the website by using the F5 key. After execution we give the input in the textboxes like
this,
Step 8
After giving the values, we click on the Submit button then the following window will appear.
Explain program to Make a Master page and apply this master page on Login page,
Registration page and Contact _us page.