chapter 3 slides asp
TRANSCRIPT
-
8/13/2019 Chapter 3 Slides asp
1/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 1
Chapter 3
How to develop
a multi-pageweb application
-
8/13/2019 Chapter 3 Slides asp
2/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 2
Objectives
Applied Given the specifications for a multi-page web application that uses anAccess data source to get data, design, code, and test the application.
To transfer to another page within a web application, be able to usethe Transfer method, the Redirect method, or cross-page posting.
To refer to pages or other files within an application, be able to useeither absolute or relative URLs.
Knowledge
Describe the contents of these special folders for ASP.NET
applications: App_Code and App_Data. Describe two ways that you can use an existing class with a new webapplication.
-
8/13/2019 Chapter 3 Slides asp
3/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 3
Objectives (continued) In general terms, describe the procedure for renaming a web form fileas well as the class that contains the code for the web form.
Distinguish between the Transfer method of the HttpServerUtilityclass, the Redirect method of the HttpResponse class, and cross-page
posting.
Distinguish between absolute and relative URLs.
Describe an Access data source.
In general terms, explain how the AccessDataSource, DataView, andDataRowView classes can be used to get data from a data source.
Describe how session state objects, session IDs, and cookies are used
to track the state of each user of a web application.
-
8/13/2019 Chapter 3 Slides asp
4/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 4
The design of the Order page
-
8/13/2019 Chapter 3 Slides asp
5/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 5
The design of the Cart page
-
8/13/2019 Chapter 3 Slides asp
6/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 6
The Solution Explorerfor the Shopping Cart application
-
8/13/2019 Chapter 3 Slides asp
7/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 7
Special folders used by the Shopping Cart
application App_Code App_Data
User folder used by the Shopping Cart application
Images
-
8/13/2019 Chapter 3 Slides asp
8/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 8
Adding a new class to the App_Code folder
-
8/13/2019 Chapter 3 Slides asp
9/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 9
Two ways to use an existing class
Add the class to your web site by copying it from another project. Add a reference to the class library that contains the class youwant.
-
8/13/2019 Chapter 3 Slides asp
10/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 10
Adding a new web form
-
8/13/2019 Chapter 3 Slides asp
11/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 11
A web form file being renamedin the Solution Explorer window
-
8/13/2019 Chapter 3 Slides asp
12/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 12
To rename the class for a renamed file
Change the name on the Class statement for the form. Change the name in the Inherits attribute of the Page directive in
the aspx code for the form.
-
8/13/2019 Chapter 3 Slides asp
13/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 13
Two methods for displaying another page
The Transfer method of the HttpServerUtility class The Redirect method of the HttpResponse class
Code that transfers control to another pageServer.Transfer("Cart.aspx")
Code that redirects the client to another pageResponse.Redirect("Cart.aspx")
-
8/13/2019 Chapter 3 Slides asp
14/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 14
Properties and methods for cross-page posting
The aspx code for a button
Code that retrieves data from the previous page protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null){TextBox txtQuantity = (TextBox)
PreviousPage.FindControl("txtQuantity");lblQuantity.Text = txtQuantity.Text;
}}
-
8/13/2019 Chapter 3 Slides asp
15/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 15
Statements that use absolute URLsResponse.Redirect("http://www.murach.com/Default.aspx")Response.Redirect("http://www.murach.com/Books/Search.aspx")
-
8/13/2019 Chapter 3 Slides asp
16/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 16
Statements that use relative URLsthat are based on the current directory
Response.Redirect("Checkout.aspx")Response.Redirect("Login/Register.aspx")
-
8/13/2019 Chapter 3 Slides asp
17/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 17
Statements that use relative URLs that navigateup the directory structure
Response.Redirect("../Register.aspx")Response.Redirect("../../Register.aspx")Response.Redirect("/Register.aspx")Response.Redirect("/Login/Register.aspx")
-
8/13/2019 Chapter 3 Slides asp
18/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 18
Server control attributes that use URLs that are
based on the root directory of the current web sitePostBackUrl="~/Cart.aspx"ImageUrl="~/Images/banner.jpg"
-
8/13/2019 Chapter 3 Slides asp
19/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 19
The Configure Data Source dialog box
-
8/13/2019 Chapter 3 Slides asp
20/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 20
The Configure Data Source wizard
-
8/13/2019 Chapter 3 Slides asp
21/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 21
The aspx code for an Access data source control
-
8/13/2019 Chapter 3 Slides asp
22/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 22
The Data Source Configuration Wizard dialog box
-
8/13/2019 Chapter 3 Slides asp
23/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 23
The aspx code for a bound drop-down list
-
8/13/2019 Chapter 3 Slides asp
24/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 24
Getting product information from the data sourceDataView productsTable = (DataView)
AccessDataSource1.Select(DataSourceSelectArguments.Empty); productsTable.RowFilter =
"ProductID = '" + ddlProducts.SelectedValue + "'";DataRowView row = (DataRowView) productsTable[0];Product p = new Product();
p.ProductID = row["ProductID"].ToString();
p.Name = row["Name"].ToString(); p.ShortDescription = row["ShortDescription"].ToString(); p.LongDescription = row["LongDescription"].ToString(); p.UnitPrice = (decimal) row["UnitPrice"]; p.ImageFile = row["ImageFile"].ToString();
Objects used DataView
DataRowView
-
8/13/2019 Chapter 3 Slides asp
25/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 25
How ASP.NET maintains the state of a session
Server First HTTP request:The browser requests a page.
ASP.NET creates a sessionstate object and assigns an IDfor the session.
Client
Web server
First HTTP response:The server returns therequested page along with the
session ID.
Next HTTP request:The browser requests another page. The server uses the sessionID included in the request to
associate the browser with thecorrect session state object.
Web server
Web server Browser
Browser
Browser Session ID
Session ID
-
8/13/2019 Chapter 3 Slides asp
26/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 26
Session state concepts
For each user session, ASP.NET creates a session state object . The session state object includes a session ID thats sent back to
the browser as a cookie .
The browser automatically returns the session ID cookie to theserver with each HTTP request.
The session ID lets the server find the right session state object. The session state object can be used to store and retrieve items
that can be used by any of the pages in the application.
-
8/13/2019 Chapter 3 Slides asp
27/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 27
Typical uses for session state
To keep information about the user , such as the users name orwhether the user has registered.
To save objects the user is working with , such as a shoppingcart or a customer record.
To keep track of pending operations , such as what steps the
user has completed while placing an order.
-
8/13/2019 Chapter 3 Slides asp
28/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 28
Common properties of the HttpSessionState class SessionID
Count
Common indexer of the HttpSessionState class [name]
Common methods of the HttpSessionState class Add(name, value)
Clear
Remove(name)
-
8/13/2019 Chapter 3 Slides asp
29/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 29
Adding to or updating a session state itemSession["Cart"] = cart;
Another way to add or update a session state itemSession.Add("Cart", cart);
Retrieving the value of a session state itemSortedList cart = (SortedList) Session["Cart"];
Removing an item from session stateSession.Remove("Cart");
-
8/13/2019 Chapter 3 Slides asp
30/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 30
Retrieving the value of a session state item from aclass that doesnt inherit System.Web.UI.Page
SortedList cart = (SortedList)HttpContext.Current.Session["Cart"];
-
8/13/2019 Chapter 3 Slides asp
31/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 31
The code for the Product classusing System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;
/// /// Summary description for Product///
public class Product{ public string ProductID; public string Name; public string ShortDescription; public string LongDescription; public decimal UnitPrice; public string ImageFile;
}
-
8/13/2019 Chapter 3 Slides asp
32/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 32
The code for the CartItem class
using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;
using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;
-
8/13/2019 Chapter 3 Slides asp
33/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 33
The code for the CartItem class (cont.)
/// /// Summary description for CartItem///
public class CartItem{
public Product Product; public int Quantity;
public string Display(){
string displayString =Product.Name + " (" + Quantity.ToString()+ " at " + Product.UnitPrice.ToString("c")+ " each)";
return displayString;}
}
-
8/13/2019 Chapter 3 Slides asp
34/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 34
The aspx file for the Order page (Order.aspx)
Chapter 3: Shopping Cart
.style1{
width: 250px;}.style2{
width: 20px;}
-
8/13/2019 Chapter 3 Slides asp
35/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 35
The aspx file for the Order page (cont.)
-
8/13/2019 Chapter 3 Slides asp
36/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 36
The aspx file for the Order page (cont.)
-
8/13/2019 Chapter 3 Slides asp
37/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 37
The aspx file for the Order page (cont.)
-
8/13/2019 Chapter 3 Slides asp
38/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 38
The aspx file for the Order page (cont.)
-
8/13/2019 Chapter 3 Slides asp
39/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 39
The aspx file for the Order page (cont.)
-
8/13/2019 Chapter 3 Slides asp
40/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 40
The code-behind file for the Order page(Order.aspx.cs)
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;
using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;
public partial class Order : System.Web.UI.Page
{ private Product selectedProduct;
-
8/13/2019 Chapter 3 Slides asp
41/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 41
The code-behind file for the Order page (cont.) protected void Page_Load(object sender, EventArgs e){
if (!IsPostBack)ddlProducts.DataBind();
selectedProduct = this.GetSelectedProduct();lblName.Text = selectedProduct.Name;lblShortDescription.Text
= selectedProduct.ShortDescription;lblLongDescription.Text
= selectedProduct.LongDescription;lblUnitPrice.Text
= selectedProduct.UnitPrice.ToString("c");imgProduct.ImageUrl = "Images/Products/"
+ selectedProduct.ImageFile;}
-
8/13/2019 Chapter 3 Slides asp
42/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 42
The code-behind file for the Order page (cont.) private Product GetSelectedProduct(){
DataView productsTable = (DataView)
AccessDataSource1.Select(DataSourceSelectArguments.Empty);
productsTable.RowFilter ="ProductID = '" + ddlProducts.SelectedValue+ "'";
DataRowView row = (DataRowView) productsTable[0];
Product p = new Product(); p.ProductID = row["ProductID"].ToString(); p.Name = row["Name"].ToString(); p.ShortDescription =
row["ShortDescription"].ToString(); p.LongDescription =
row["LongDescription"].ToString();
p.UnitPrice = (decimal) row["UnitPrice"]; p.ImageFile = row["ImageFile"].ToString();
return p;}
-
8/13/2019 Chapter 3 Slides asp
43/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 43
The code-behind file for the Order page (cont.) protected void btnAdd_Click(object sender, EventArgs e){
if (Page.IsValid){
CartItem item = new CartItem();item.Product = selectedProduct;item.Quantity =
Convert.ToInt32(txtQuantity.Text);this.AddToCart(item);Response.Redirect("Cart.aspx");
}}
-
8/13/2019 Chapter 3 Slides asp
44/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 44
The code-behind file for the Order page (cont.) private void AddToCart(CartItem item){
SortedList cart = this.GetCart();
string productID = selectedProduct.ProductID;if (cart.ContainsKey(productID)){
CartItem existingItem =(CartItem) cart[productID];
existingItem.Quantity += item.Quantity;}
elsecart.Add(productID, item);
}
private SortedList GetCart(){if (Session["Cart"] == null)
Session.Add("Cart", new SortedList());
return (SortedList) Session["Cart"];}
}
-
8/13/2019 Chapter 3 Slides asp
45/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 45
The aspx file for the Cart page (Cart.aspx)
Chapter 3: Shopping Cart
.style1 { width: 286px;
height: 153px;}.style2 {
height: 153px;}
-
8/13/2019 Chapter 3 Slides asp
46/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 46
The aspx file for the Cart page (cont.)
Your shopping cart:
-
8/13/2019 Chapter 3 Slides asp
47/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 47
The aspx file for the Cart page (cont.)
-
8/13/2019 Chapter 3 Slides asp
48/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 48
The code-behind file for the Cart page(Cart.aspx.cs)
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;
public partial class Cart : System.Web.UI.Page{
private SortedList cart;
-
8/13/2019 Chapter 3 Slides asp
49/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 49
The code-behind file for the Cart page (cont.) protected void Page_Load(object sender, EventArgs e){
this.GetCart();if (!IsPostBack)this.DisplayCart();
}
private void GetCart(){if (Session["Cart"] == null)
Session.Add("Cart", new SortedList());
cart = (SortedList) Session["Cart"];}
private void DisplayCart(){lstCart.Items.Clear();CartItem item;foreach (DictionaryEntry entry in cart)
{ item = (CartItem) entry.Value;lstCart.Items.Add(item.Display());
}}
-
8/13/2019 Chapter 3 Slides asp
50/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 50
The code-behind file for the Cart page (cont.) protected void btnRemove_Click(
object sender, EventArgs e){
if (cart.Count > 0){if (lstCart.SelectedIndex > -1){
cart.RemoveAt(lstCart.SelectedIndex);this.DisplayCart();
}
else{lblMessage.Text = "Please select the item "
+ "you want to remove.";}
}}
-
8/13/2019 Chapter 3 Slides asp
51/52
Murachs ASP.NET 3.5/C#, C3 2008, Mike Murach & Associates, Inc. Slide 51
The code-behind file for the Cart page (cont.) protected void btnEmpty_Click(
object sender, EventArgs e){
cart.Clear();lstCart.Items.Clear();}
protected void btnCheckOut_Click(object sender, EventArgs e){
lblMessage.Text = "Sorry, that function "
+ "hasn't been implemented yet.";}
}
-
8/13/2019 Chapter 3 Slides asp
52/52
Sorted list concepts Each item in a SortedList object is a DictionaryEntry object.
A DictionaryEntry object consists of a key and value.
The Value property of a DictionaryEntry object returns an Objecttype so it must be cast to the proper type before it can be stored.