You are on page 1of 21

Developing Web

Applications Using
Microsoft® Visual
Studio® 2008
Module 3: Creating a Microsoft ASP.NET Web Form
• Creating Web Forms

• Adding Server Controls to a Web Form

• Creating Master Pages


Lesson: Creating Web Forms
• What Is a Web Form?

• Creating a Web Form by Using Visual Studio 2008


What Is a Web Form?
Web Forms have the following features:
• .aspx extension
• Page attributes
 @ Page directive
• Body attributes
• Form attributes

<%@ Page Language=“C#" CodeFile=“NewForm.aspx.cs"


Inherits=“NewForm" %>
<html>
<body>
<form id=“form1" runat="server">
</form>
</body>
</html>
Creating a Web Form by Using Visual Studio 2008
• Create a new Web application

 Default Web Form for a new application: Default.aspx

• Add additional Web Forms to an existing application

 In Solution Explorer user the Add New Item dialog box


Lesson: Adding Server Controls to a Web Form
• What Is a Server Control?

• Types of Server Controls

• Saving View State

• HTML Server Controls

• Web Server Controls

• Discussion: Selecting the Appropriate Control


What Is a Server Control?

<asp:Button ID="Button1" runat="server" Text="Submit"/>

Server controls:
• Contain runat="server" attribute
 Events happen on the server
 View state saved

• Provide built-in functionality

• Provide a common object model


 All have ID and Text attributes

• Create browser-specific HTML


Types of Server Controls
• HTML server controls

• Web server controls


 Intrinsic controls
 Validation controls
 Rich controls
 List-bound controls
 Login controls
Saving View State
• View state

<asp:ListBox ID="ListName"
EnableViewState="true" runat="server">
</asp:ListBox>

• Control state
 Use view state to maintain the control's contents
 Use control state to maintain the core behavior of the
control

• HiddenField control
HTML Server Controls
• Based on HTML elements

• Exist in the
System.Web.UI.HtmlControls
namespace

<input type="text" id="txtName"


runat="server" />
Web Server Controls
• Exist in the
System.Web.UI.WebControls
namespace
Control syntax

<asp:TextBox id="TextBox1"
runat="server">Text_to_Display
</asp:TextBox>

HTML that is generated by the control

<input name="TextBox1" type="text"


value="Text_to_Display"
Id="TextBox1"/>
Discussion: Selecting the Appropriate Control
• Is it ever appropriate to use HTML server controls instead
of Web server controls?
Lesson: Creating Master Pages
• What Are Master Pages?

• Content Pages

• Runtime Behavior of Master Pages

• Discussion: Advantages of Master Pages


What Are Master Pages?
• Master pages provide a simple method to create a
consistent layout across a Web site
• Master pages have the following features:
 File extension of .master instead of .aspx
 @ Master directive instead of the @ Page directive
 ContentPlaceHolder controls, which define the areas
where the replaceable content appears
 Top-level HTML elements for a page, such as html,
head, and form

• Master pages can be nested


Content Pages
• Content pages are Web pages that reference a
master page
 MasterPageFile attribute in @ Page directive

• Content pages have their own page-specific


content

<%@ Page Language="C#"


MasterPageFile="~/benefitsMaster.master"
AutoEventWireup="true" CodeFile="contentPage.aspx.cs"
Inherits="contentPage" %>

<%@ Page Language=“VB"


MasterPageFile="~/benefitsMaster.master"
AutoEventWireup="true" CodeFile="contentPage.aspx.vb"
Inherits="contentPage" %>
Runtime Behavior of Master Pages

Master Page Content Page

Resulting Web Page


Discussion: Advantages of Master Pages
• What are the advantages of master pages in a
Web application?
Lab: Creating a Microsoft ASP.NET Web Form
• Exercise 1: Creating the Default.aspx Web Form

• Exercise 2: Creating the benefitsMaster Master Page

• Exercise 3: Creating the life.aspx Web Form

Logon information

Virtual machine 2310C-LON-DEV-03


User name Student
Password Pa$$w0rd

Estimated time: 30 minutes


Lab Scenario

Master Page
Logon Page benefitsMaster.master
login.aspx
Benefits Lab Web
Home Page Application
ASPState
Default.aspx Page Header
header.ascx
Menu Component
Registration Benefits.cs or Benefits.vb
register.aspx TempDB
Web.
config

Life Insurance Retirement Medical Dentists


life.aspx retirement.aspx medical.aspx dental.aspx

Prospectus
prospectus.aspx Doctors User Control XML Web
LINQ to SQL doctors.aspx nameDate.ascx Service
Classes DentalService1.asmx
Doctors.dbml

Doctors Dentists
XML Files
Lab Review
Review Questions
• What categories of server controls are used in the lab?

• What content in the life.aspx page comes from the


master page?
• Which other pages in the application can use the
master page?
Module Review and Takeaways
• Review Questions

• Real-World Issues and Scenarios

• Best Practices

You might also like