a2f25a331b07a0e0ce7676f469fdae2c.ppt
- Количество слайдов: 11
Chapter 3: Develop A Multi-Page Web Application Liu, Jie Professor Department of Computer Science Western Oregon University
The Store Application § A Halloween Store – online retail • One page for viewing the products § Accessing a database § Switch to a different page • One page to view the shopping cart § Share information from the previous page § The Code
Some Skills § Add a class (OOP) • Add an existing class § Right click on Solution Explorer Add Existing Item • Add a New class § Right click on Solution Explorer Add Existing Item • § Add another page • § Need to change the class name and “Inherits” directive manually Set the start page • • § Right click on Solution Explorer Add New Item, select Web Form, can provide the name of your new page Change the name of a page • § Refer a class in the class library Single form app, the form is displayed Or multi-form app, the selected/set one is displayed Or the default. aspx Or the directory listing -- need to set a start page Redirect/Transfer to a different page • • Transfer (URL) -- ASPX only, fast because the shift happens at server, address not reflect the actual page Redirect (URL) – any page, less efficient because the shift happens at the client
Some Skills – Cross Page Posting § This is a different approach to move to a different page • The new page’s URL is set in the Post. Back. Url property of a button control • Clicking the button results in the loading of a different page specified in the Post. Back. Url property • You may have access of the “Previous” Page and controls in the previous page • This tool is used when no user input needs to be processed
Some Skills – Cross Page Posting § Code for the button • Post. Back. Url = “~/Page. B. aspx” Page. A Page. B § Code to refer a control in the previous page Cross-page posting Button Previous Page Protected void Page_Load(object sender, Event. Args e) { if (Previous. Page != null) { Text. Box txt. QTY = (Text. Box) Previous. Page. Find. Control(“txt. QTY”); lbl. QTY. Text = txt. QTY. Text; } }
Absolute and Relative URLs § Absolute URLs – § Relative URLs – include domain name specify the page relative to the current page URL Comment http: //www. abc. com/default. aspx Absolute URL Checkout. aspx Relative URL to a file in the current directory /Gold. Member/Checkout. aspx Relative URL to a file in a sub-directory . . /Register. aspx Relative URL at one level up . . /Register. aspx Relative URL at two levels up /Register. aspx Relative URL at the root Post. Back. URL = “~/Intro. aspx” Relative URL at the root on controls
About Data Sources § Assume you have a MS SQL Server DB § Having the data to show in a form is very easy – using the smart tag § Work on the smart tag menu to configure • Select database • Select retrieved data/columns
About Data Sources §
Using Data § More on later sections § Make sure the control allows postback § Make sure to bind the control with the data source § Use a Dataview to get the row and column names to get the values in the row
The Code protected void Page_Load(object sender, Event. Args e) { if (!Is. Post. Back) Drop. Down. List 1. Data. Bind(); } protected void Drop. Down. List 1_Selected. Index. Changed(object sender, Event. Args e) { Product selected. Product; // show to use an object to store a row if (Drop. Down. List 1. Selected. Value != "") { Data. View dv. Product = (Data. View) dts. Product. Select(Data. Source. Select. Arguments. Empty); dv. Product. Row. Filter = "Product. ID = '" + Drop. Down. List 1. Selected. Value + "'"; Data. Row. View row = (Data. Row. View)dv. Product[0]; selected. Product = new Product(); // initial the obj selected. Product. Unit. Price = (decimal)row["Unit. Price"]; selected. Product. Long. Description = row["Long. Description"]. To. String(); selected. Product. Image. File = row["Image. File"]. To. String(); lbl. Description. Text = selected. Product. Long. Description; lbl. Price. Text = selected. Product. Unit. Price. To. String("c"); img. Product. Image. Url = "Images/Products/" + selected. Product. Image. File; } }
Using Session State § The difference between an Application and a Session § Session. ID – unique for each session and kept when a session state is added § It is stored as key/value pair with value being an object • • Session[“Cart”] = cart; // cart can be anything, a Sorted. List here Session. Add (“Cart”, cart); Sorted. List cart = (Sorted. List) Session[“Cart”]; Sorted. List cart = (Sorted. List) Http. Context. Current. Session[“Cart”]; § Major Property: Session. ID, Count, Timeout § Methods: Abandon, Clear, Remove
a2f25a331b07a0e0ce7676f469fdae2c.ppt