Default.asp
<!DOCTYPE html>
<html>
<head>
<title>Web Pages Demo</title>
</head>
<body>
<h1>Welcome to W3Schools</h1>
<h2>Web Site Main Ingredients</h2>
<p>A Home Page (Default.cshtml)</p>
<p>A Layout File (Layout.cshtml)</p>
<p>A Style Sheet (Site.css)</p>
</body>
</html>
Run example
About.cshtml
<!DOCTYPE html>
<html>
<head>
<title>About</title>
</head>
<body>
<h1>About Us</h1>
<p>Lorem Ipsum Porem Lorem Ipsum Porem</p>
</body>
</html>
The about file above contains a heading and a paragraph. Please feel free to edit the content.
What We Will Do
In this chapter we will:
Site.css
body
{
font: "Trebuchet MS", Verdana, sans-serif;
background-color: #5c87b2;
color: #696969;
}
#main
{
padding: 30px;
background-color: #ffffff;
border-radius: 0 0 4px 4px;
}
h1
{
font: Georgia, serif;
border-bottom: 3px solid #cc9900;
color: #996600;
}
The CSS file above defines the styles to be used used for
In your web folder (DemoWebPages), create a new CSHTML file named "Layout.cshtml".
Put the following code inside the layout file:
Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="Site.css">
</head>
<body>
<div id="main">
@RenderBody()
<p>© 2013 W3Schools. All rights reserved.</p>
</div>
</body>
</html>
The layout file above defines the layout of your web pages. It has a link to your style sheet file
(Site.css), and a call to the @RenderBody() function, where your page content should be
displayed.
Default.cshtml
@{Layout="Layout.cshtml";}
<h1>Welcome to W3Schools</h1>
<h2>Web Site Main Ingredients</h2>
<p>A Home Page (Default.cshtml)</p>
<p>A Layout File (Layout.cshtml)</p>
<p>A Style Sheet (Site.css)</p>
Run example
The file starts with a reference to the layout file, otherwise it contains normal HTML markup.
Congratulations
You have created your first web site, with a main page (Default.cshtml), a common template for
all your pages (Layout.cshtml), and a common style sheet (Sire.css).
In the next chapters of this tutorial, we will add a database to the web site. Then we will add
login and security, and finally add navigation.
What We Will Do
In this chapter we will:
If you want to learn how to create a database for your web, please go to the chapter Web
Database.
Customers.cshtml
@{Layout = "Layout.cshtml";
var db = Database.Open("Northwind");
var query = db.Query("SELECT CompanyName,City,Country FROM Customers");
}
<html>
<body>
<h1>Customers</h1>
<table>
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
@foreach(var row in query)
{
<tr>
<td>@row.CompanyName</td>
<td>@row.City</td>
<td>@row.Country</td>
</tr>
}
</table>
</body>
</html>
Run example
Database Connection
The Database.Open(name) method will connect to a database in two steps:
First, it searches the application's App_Data folder for a database that matches the name
parameter without the file-name extension.
If no file is found, it looks for a "connection string" in the application's Web.config file.
(A connection string contains information about how to connect to a database. It can include a
file path, or the name of an SQL database, with full user name and password)
This two-step search makes it possible to test the application with a local database, and run the
application on a web host using a connection string.
What We Will Do
In this chapter we will:
_AppStart.cshtml
@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);
}
The code above will be run each time your web site (application) starts. It will initialize a
WebSecurity database, with user profiles, logins, passwords, and membership information (if it
is not already initialized).
"Users" is the name of the database.
"UserProfile" is the name of the database table that contains the user profile information.
"UserId" is the name of the (integer) column that contains the user IDs.
"Email" is the name of the (string) column that contains user names.
true is a boolean value indicating that the user profile and membership tables should be created
automatically if they don't exist. Otherwise set to false.
Although true indicates that the membership tables can be created automatically, the
database itself must always exist.
Site.css
/* Forms */
fieldset label
{
display:block;
padding:4px;
}
input[type="text"],input[type="password"]
{
width:300px;
}
input[type="submit"]
{
padding:4px;
}
Register.cshtml
@{Layout = "Layout.cshtml";
// Initialize page
var email = "";
<legend>Sign-up Form</legend>
<ol>
<li>
<label>Email:</label>
<input type="text" id="email" name="email" />
</li>
<li>
<label>Password:</label>
<input type="password" id="password" name="password" />
</li>
<li>
<label>Confirm Password:</label>
<input type="password" id="confirmPassword" name="confirmPassword" />
</li>
<li>
<p><input type="submit" value="Register" /></p>
</li>
</ol>
</fieldset>
</form>
Login.cshtml
@{Layout="Layout.cshtml";
// Initialize page
var username = "";
var password = "";
var ErrorMessage = "";
// If this is a POST request, validate and process data
if (IsPost)
{
username = Request.Form["username"];
password = Request.Form["password"];
if (username.IsEmpty() || password.IsEmpty())
{ErrorMessage = "You must specify a username and password.";}
else
{
// Login, Navigate back to the homepage and exit
if (WebSecurity.Login(username, password, false))
{Response.Redirect("Default.cshtml");}
else
{ErrorMessage = "Login failed";}
}
}
if (ErrorMessage!="")
{
<p>@ErrorMessage</p>
<p>Please correct the errors and try again.</p>
}
}
<h1>Login</h1>
<form method="post" action="">
<fieldset>
<legend>Log In to Your Account</legend>
<ol>
<li>
<label>Username:</label>
<input type="text" id="username" name="username" />
</li>
<li>
<label>Password:</label>
<input type="password" id="password" name="password" />
</li>
<li>
<p><input type="submit" value="login" /></p>
</li>
</ol>
</fieldset>
</form>
Congratulations
You have added membership registration and login information to your website.
Previous
Next Chapter
What We Will Do
In this chapter we will:
Security Code:
if (!WebSecurity.IsAuthenticated)
{
Response.Redirect("Login.cshtml");
}
The code above executes an if test, asking if the user is logged in. If not, the user is redirected to
the login page.
Customers.cshtml
@{
if (!WebSecurity.IsAuthenticated)
{
Response.Redirect("Login");
}
Layout = "Layout.cshtml";
var db = Database.Open("Northwind");
var query = db.Query("SELECT CompanyName,City,Country FROM Customers");
}
<html>
<body>
<h1>Customers</h1>
<table>
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
@foreach(var row in query)
{
<tr>
<td>@row.CompanyName</td>
<td>@row.City</td>
<td>@row.Country</td>
</tr>
}
</table>
</body>
</html>
The Customers page above, is a copy of the page from the previous chapter about
databases. The security code (added at the beginning) is marked red.
Congratulations
You have added security to your web site, using the WebSecurity object.
For a full reference to the WebSecurity object, please visit our WebSecurity Object Reference.
What We Will Do
In this chapter we will:
Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="Site.css">
</head>
<body>
<ul id="menu">
<li><a href="Default">Home</a></li>
<li><a href="About">About</a></li>
<li><a href="Customers">Data</a></li>
<li><a href="Register">Register</a></li>
<li><a href="Login">Login</a></li>
</ul>
<div id="main">
@RenderBody()
<p>© 2013 W3Schools. All rights reserved.</p>
</div>
</body>
</html>
The layout file above, is a copy of the layout file from the previous chapter, with an added
unordered list.
Site.css
body
{
font: "Trebuchet MS", Verdana, sans-serif;
background-color: #5c87b2;
color: #696969;
}
#main
{
padding: 30px;
background-color: #ffffff;
border-radius: 0 0 4px 4px;
}
h1
{
font: Georgia, serif;
border-bottom: 3px solid #cc9900;
color: #996600;
}
/* Forms */
fieldset label
{
display:block;
padding:4px;
}
input[type="text"],input[type="password"]
{
width:300px;
}
input[type="submit"]
{
padding:4px;
}
/* Menu */
ul#menu
{
padding: 0px;
position: relative;
margin: 0;
}
ul#menu li
{
display: inline;
}
ul#menu li a
{
background-color: #ffffff;
padding: 10px 20px;
text-decoration: none;
line-height: 2.8em;
color: #034af3;
border-radius: 4px 4px 0 0;
}
ul#menu li a:hover
{
background-color: #e8eef4;
}
Run example
The style sheet above, is a copy of the style sheet from the previous chapter, with added styles
for an unordered list (marked red), and a small change in the border-radius style for the main
window.
Congratulations
You have added navigation to your website.
What We Will Do
In this chapter we will:
getCustomers.cshtml
@{
var db = Database.Open("Northwind");
var query = db.Query("SELECT CompanyName,City,Country FROM Customers");
}
<table border="1">
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
@foreach(var row in query)
{
<tr>
<td>@row.CompanyName</td>
<td>@row.City</td>
<td>@row.Country</td>
</tr>
}
</table>
The file above is a standard CSHTML file, returning only an HTML table.
Ajax.html
<!DOCTYPE html>
<html>
<head>
<title>ASP Demo</title>
<link href="Site.css" rel="stylesheet">
</head>
<body>
<div id="main">
<h1>Customers</h1>
<div id="t01"></div>
</div>
<script>
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("t01").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getCustomers.cshtml",true);
xmlhttp.send();
</script>
</body>
</html>
Try it Yourself
Congratulations
You have created your first data page using AJAX.
Default.aspx
<!DOCTYPE html>
<html>
<head>
<title>ASP Web Forms Demo</title>
</head>
<body>
<h1>Welcome to W3Schools</h1>
<h2>Web Site Main Ingredients</h2>
<p>A Home Page (Default.aspx)</p>
<p>A Style Sheet (Site.css)</p>
<p>A Layout File (Site.master)</p>
</body>
</html>
Run example
About.aspx
<!DOCTYPE html>
<html>
<head>
<title>About</title>
</head>
<body>
<h1>About Us</h1>
<p>Lorem Ipsum Porem Lorem Ipsum Porem</p>
</body>
</html>
The about file above contains a heading and a paragraph. Please feel free to edit the content.
What We Will Do
In this chapter we will:
Site.css
body
{
font: "Trebuchet MS", Verdana, sans-serif;
background-color: #5c87b2;
color: #696969;
}
#main
{
padding: 30px;
background-color: #ffffff;
border-radius: 0 0 4px 4px;
}
h1
{
font: Georgia, serif;
border-bottom: 3px solid #cc9900;
color: #996600;
}
The CSS file above defines the styles to be used used for
Site.master
<%@ Master Language="VB" %>
<!DOCTYPE html>
<html>
<head id="head" runat="server">
<title></title>
<link href="Site.css" rel="stylesheet">
</head>
<body>
Default.aspx
<%@ Page Title="Home" MasterPageFile="Site.master" %>
<asp:Content ID="Content" ContentPlaceHolderID="MainContent" runat="server">
<h1>Welcome to W3Schools</h1>
<h2>Web Site Main Ingredients</h2>
<p>A Home Page (Default.aspx)</p>
<p>A Style Sheet (Site.css)</p>
<p>A Layout File (Site.master)</p>
</asp:Content>
Run example
The file starts with a reference to the layout file, otherwise it contains an asp control and a
normal HTML markup.
About.aspx
<%@ Page Title="Home" MasterPageFile="Site.master" %>
<asp:Content ID="Content" ContentPlaceHolderID="MainContent" runat="server">
<h1>About Us</h1>
<p>Lorem Ipsum Porem Lorem Ipsum Porem</p>
</asp:Content>
Congratulations
You have created your first ASP.NET Web Forms site, with a main page (Default.aspx), a
common template for all your pages (Site.master), and a common style sheet (Site.css).
In the next chapters of this tutorial, we will add a database to the web site. And finally we will
add navigation.
Description
HTML Pages
Web pages to be viewed directly by the browser
Style Sheets
Defines styles and formatting for the web site
JavaScripts
Contains JavaScripts to be executed in the browser
Web Forms
The core of ASP.NET. Web pages with ASP.NET code.
Web User Controls
Contains code fragments to be used by multiple pages
Global Class
Global code (like startup code)
Web Service
Contains callable web services
Class Files
Contains code in C#
.vb
.config
.master
.sitemap
.xml
.sdf
.mdf
Class Files
Contains code in Vb (Visual Basic)
Web Configuration
Contains configuration information for the web site
Master Pages
Define the global structure (look) of the web site
XML Sitemap
Data stored in XML format
SQL Server Compact Databases
SQL Server Express (Access) Databases
What We Will Do
In this chapter we will:
Customers.aspx
<%@ Page Title="Data" MasterPageFile="Site.master" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
Run example
Congratulations
You have added a database to your web project.
What We Will Do
In this chapter we will:
Site.master
<%@ Master Language="VB" %>
<!DOCTYPE html>
<head id="head" runat="server">
<title></title>
<link href="Site.css" rel="stylesheet">
</head>
<body>
<form id="form1" runat="server">
<ul id="menu">
<li><a href="Default.aspx">Home</a></li>
<li><a href="Customers.aspx">Data</a></li>
<li><a href="About.aspx">About</a></li>
</ul>
<div id="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
<p>© <%=DateTime.Now.year%> W3Schools. All rights reserved.</p>
</div>
</form>
</body>
</html>
The master file above, is a copy of the master file from the previous chapter, with an added
unordered list (marked red).
Site.css
body
{
font: "Trebuchet MS", Verdana, sans-serif;
background-color: #5c87b2;
color: #696969;
}
#main
{
padding: 20px;
background-color: #ffffff;
border-radius: 0 4px 4px 4px;
}
h1
{
font: Georgia, serif;
border-bottom: 3px solid #cc9900;
color: #996600;
}
ul#menu
{
padding: 0px;
position: relative;
margin: 0;
}
ul#menu li
{
display: inline;
}
ul#menu li a
{
background-color: #ffffff;
padding: 10px 20px;
text-decoration: none;
line-height: 2.8em;
color: #034af3;
border-radius: 4px 4px 0 0;
}
ul#menu li a:hover
{
background-color: #e8eef4;
}
Run example
The style sheet above, is a copy of the style sheet from the previous chapter, with added styles
for an unordered list (marked red).
Congratulations
You have added navigation to your website.
What We Will Do
In this chapter we will:
getCustomers.aspx
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%
Dim dbconn As OleDbConnection
Dim objAdapter As OleDbDataAdapter
Dim objTable As DataTable
Dim objRow As DataRow
Dim objDataSet As New DataSet()
dbconn=New OledbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data
source=C:\WebData\Northwind.mdb")
objAdapter=New OledbDataAdapter("SELECT CompanyName,City,Country FROM
Customers",dbconn)
objAdapter.Fill(objDataSet,"customers")
objTable=objDataSet.Tables("customers")
objRow=objTable.Rows(0)
response.write("<table border='1'>")
response.write("<tr><th>Name</th><th>City</th><th>Country</th></tr>")
for each x in objTable.Rows
response.write("<tr>")
response.write("<td>" & x("companyname") & "</td>")
response.write("<td>" & x("city") & "</td>")
response.write("<td>" & x("country") & "</td>")
response.write("</tr>")
next
response.write("</table>")
dbconn.Close()
%>
The file above is a standard ASPX file, returning only an HTML table.
Ajax.html
<!DOCTYPE html>
<html>
<head>
<title>ASP Demo</title>
<link href="Site.css" rel="stylesheet">
</head>
<body>
<div id="main">
<h1>Customers</h1>
<div id="t01"></div>
</div>
<script>
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
Congratulations
You have created your first data page using AJAX.