COMP 3241 EBusiness Technologies Richard Henson University of

  • Slides: 42
Download presentation
COMP 3241 E-Business Technologies Richard Henson University of Worcester November 2014

COMP 3241 E-Business Technologies Richard Henson University of Worcester November 2014

Week 6: Coding a C# Shopping Cart (!) n By the end of this

Week 6: Coding a C# Shopping Cart (!) n By the end of this session you will be able to: Øexplain the structure of a. net environment application including controls, assemblies, code-behind file(s), design file(s), web. config Øapply asp. net principles to the creation of a shopping system ØExplain how the shopping cart dataset interfaces with the products table and update smoothly when user changes their mind

C# and “classes” “C” has been around since 1972 n C++ is the object-orientated

C# and “classes” “C” has been around since 1972 n C++ is the object-orientated (OO) version, invented in 1983… n C# is even more OO n All versions of C are compiled n Øi. e. code runs as machine code or assembly language; source code must be compiled beforehand…

Classes n A bit like entities in systems analysis… Ø can have “properties” Ø

Classes n A bit like entities in systems analysis… Ø can have “properties” Ø equivalent to attributes… n Have to be carefully coded… Ø could be “public” or “private” Ø properties are variables » should really be “private” Ø code snippets (methods) – public

Properties & Objects n Need to “instantiate” a newly-defined class by giving it “objects”

Properties & Objects n Need to “instantiate” a newly-defined class by giving it “objects” (hence OBJECT-ORIENTATED)… Øe. g. if a class is defined with colour as a property Øthen a single value “red” would be enough to create an “object” of that class

C# Constructors & Methods n Sounds terrible… Ø actually just the different [public] code

C# Constructors & Methods n Sounds terrible… Ø actually just the different [public] code segments associated with a class n n A constructor uses property(ies) to define or “construct” a member/object of that class Methods can include any programming structures and do stuff using property values

“Partial” Classes and Methods n Partial Classes Ø used to describe a situation where

“Partial” Classes and Methods n Partial Classes Ø used to describe a situation where different parts of a class are defined in different places n Partial Methods Ø within a partial class Ø must return a “void” value Ø implicitly “private”

C# classes & Code Behind n When. aspx page is requested and it renders

C# classes & Code Behind n When. aspx page is requested and it renders its markup code to a browser… Ø ASP. NET generates & compiles the “classes” that perform the tasks required to run the page

“Classes” and the code behind model n the. net framework also: Ø sets up

“Classes” and the code behind model n the. net framework also: Ø sets up the JIT runtime environment Ø converts source code to “intermediate language” (IL)

“Classes” and the code behind model n n Combination of different code What runs

“Classes” and the code behind model n n Combination of different code What runs is not just the code that was created for the. aspx page plus its associated “code behind” (!)

Generating a class, as Code Behind n When a. aspx page runs, it has

Generating a class, as Code Behind n When a. aspx page runs, it has already combined: Ø the server-side elements in the page » e. g. toolbox controls Ø the event-handling code written as “code behind” n The class or classes that the compiler creates depends on whether the page uses the single -file model or the code-behind model Ø no need to pre-compile such pages into assemblies!

Generating and Running the Page’s “class” Code n . NET framework dynamically compiles &

Generating and Running the Page’s “class” Code n . NET framework dynamically compiles & runs. aspx pages the first time user requests them: Ø IF…any changes are subsequently made » either to the page » or resources the page depends on Ø THEN… the page is automatically recompiled n . NET also supports precompilation of a Web site: Ø to enhance performance Ø to perform error checking Ø to support site deployment

Single-File Pages n When the page is compiled: Øthe compiler generates then compiles… ØEITHER

Single-File Pages n When the page is compiled: Øthe compiler generates then compiles… ØEITHER » a new class that derives from the base Page class ØOR » a custom base class defined with the Inherits attribute of the @ Page directive.

Single-File Pages ØExample: » IF… a new ASP. NET Web page named Sample. Page

Single-File Pages ØExample: » IF… a new ASP. NET Web page named Sample. Page 1. aspx is created in application root directory » THEN… a new class named ASP. Sample. Page 1_as px will be derived from the Page class

Single-File Pages n IF that single-file is inside an application subfolder Øsubfolder name is

Single-File Pages n IF that single-file is inside an application subfolder Øsubfolder name is used as part of the generated class & contains: » declarations for the controls in the. aspx page » the event handlers and other custom code

After Page generation… n Generated class… Øcompiled into an assembly Øassembly loaded into the

After Page generation… n Generated class… Øcompiled into an assembly Øassembly loaded into the application domain n Page class… Øinstantiated Øexecuted Ørenders output to the browser…

Effect of Page modifications n IF a page changes & this would affect the

Effect of Page modifications n IF a page changes & this would affect the generated class… e. g. Øadding controls Ømodifying code n THEN Øcompiled class code is invalidated Øa new class is generated

Using C# classes with “event-driven” controls EXAMPLE (an “add to cart” button): protected void

Using C# classes with “event-driven” controls EXAMPLE (an “add to cart” button): protected void Data. List 1_Item. Command (objectsource, Data. List. Command. Event. Args e) { if (e. Command. Name == "Add. To. Cart") { // Add code here to add the item to the shopping cart. // Use the value of e. Item. Index to retrieve the data // item in the control. } }

Use of Multiple Scripting languages in a Page n General rule… DON’T! Ø use

Use of Multiple Scripting languages in a Page n General rule… DON’T! Ø use of multiple compilers wastes system resources n If you must: Ø HTML <SCRIPT> tag and its LANGUAGE added within the. aspx code » usually placed after the <HEAD></HEAD> tags » RUNAT attributes (where code is executed) optional Ø Syntax: » <SCRIPT LANGUAGE=Scripting. Language RUNAT=Server> n script… » </SCRIPT> » similar to embedded client code

Keep it simple! n Multiple languages not even recommended for different pages in the

Keep it simple! n Multiple languages not even recommended for different pages in the same application!!! Ømakes the ASP. net engine load multiple scripting engines Øneeds more processing » which piece of script should be sent to which scripting engine! Øreduces the responsiveness and scalability of the application

Potential Scripting Problems in a RAD Environment like Vis Studio… n Correct scripting language

Potential Scripting Problems in a RAD Environment like Vis Studio… n Correct scripting language always needs to be defined at the start of the page Ø dynamic “new” pages provide a range of options n Inserted controls: Ø can be inserted in the wrong place… Ø can themselves become muddled (start again? ) Ø may already contain HTML code or (if database) SQL code

Potential Problems of Scripting in the VSt Environment… (cont. ) n Embedded server script

Potential Problems of Scripting in the VSt Environment… (cont. ) n Embedded server script code may depend on a link to a database Ø link needs to be functioning correctly… Ø database field types must correspond with script equivalents n If the database tables/fields change significantly, the script must accommodate the field changes… Ø roll out changes carefully Ø may be easier to recreate control or whole page…

Use of Comments in aspx files n Apostrophe for VBScript Ø ‘This is a

Use of Comments in aspx files n Apostrophe for VBScript Ø ‘This is a VBScript comment n Three slashes for C# n An excellent C# resource: Ø /// <summary> Ø /// The “my. Class” class represents an arbitrary class Ø /// </summary> Ø http: //www. softsteel. co. uk/tutorials/c. Sharp/cont ents. html

More about Objects n In general terms… chunks of server script code that will

More about Objects n In general terms… chunks of server script code that will together provide web functionality Ø using RAD tools, added to a dynamic HTML page “at the click of a mouse button” Ø available in a number of languages » asp, asp. net » php, cold fusion n Several objects usually used together to create a useful web application e. g. Ø data capture form and sending data to database Ø presentation of data from a database

“Components” and “Objects” n Very similar and easily confused… ØObject » part of the

“Components” and “Objects” n Very similar and easily confused… ØObject » part of the library of files available to the whole application ØComponent » a number of objects » a mini-application in its own right » any instance of a component must be explicitly created before it can be used

Using COM in aspx files n COM (later COM+) components… Ø popular with developers…

Using COM in aspx files n COM (later COM+) components… Ø popular with developers… Ø provided consistent, reusable functionality Ø easy to maintain and share Ø could be modified or enhanced without affecting the rest of the application n Microsoft therefore reluctant to give up a good thing and encouraged using COM+ with. net Ø http: //msdn 2. microsoft. com/en-us/library/bb 735856. aspx

ASP. net and directing user input · The “Http. Request” class includes… · event

ASP. net and directing user input · The “Http. Request” class includes… · event handling controls · web controls · · Web controls can also be pre-written and pre-compiled as assembles… Really useful in designing pages to handle user input

How a click on the product can update the cart n Essential that the

How a click on the product can update the cart n Essential that the cart dataset is available with each product page Ø otherwise, product and quantity data can’t be added to the cart! n Advice: use “split screen” to see the code as well as the screen design Ø then able to check the code that corresponds with the cart, and its appropriateness for page display Ø if there is a mismatch, you can’t expect the shopping cart to be updated…

Web Controls and ASP. net Shopping Carts n Web controls…object classes used by asp.

Web Controls and ASP. net Shopping Carts n Web controls…object classes used by asp. net to render parts of a page Ø similar to HTML “forms” Ø extra advantage of being reusable n An. aspx web page may use a hierarchical structure of defined web controls Ø making a combination of code behind assembly-based controls easily manageable

Some Typical Web Controls that used with a HTML form button n checkboxlist n

Some Typical Web Controls that used with a HTML form button n checkboxlist n dropdownlist n imagebutton n listbox radiobuttonlist textbox label

Web Control syntax (1) n Textbox & contents must be held within a HTML

Web Control syntax (1) n Textbox & contents must be held within a HTML form Ø i. e. between <form> and </form> n Opening tag: <asp: textbox> Ø various properties of the control follow… » e. g. display of text box » displayed text contents » runat=“server” n Closing tag: /> or </asp: textbox>

Dropdown box control & “nesting” syntax n Drop Down List box & contents Ø

Dropdown box control & “nesting” syntax n Drop Down List box & contents Ø again within a HTML form n Opening tag: <asp: dropdownlist>… 1. properties of the control individually defined e. g. properties of drop down list box, and displayed text contents runat=“server” (essential) Controlname ID = “unique name” (also essential) 2. further nested control needed for each item in the drop down list…

Dropdown box control & “nesting” syntax (cont…) 3. Each item defined as: <asp: listitem>

Dropdown box control & “nesting” syntax (cont…) 3. Each item defined as: <asp: listitem> …. properties of listitem…. </asp: listitem> properties should include: text = “” (what is displayed) value = “” (what is passed on) 4. Closing tag: </asp: dropdownlist> or />

Dropdown box control & “nesting” syntax (3) n Typical code for whole structure: Ø

Dropdown box control & “nesting” syntax (3) n Typical code for whole structure: Ø Note the embedding of one control, listitem within another, dropdownbox <form> <asp: dropdownlist ID = “selected” runat = “server”> <listitem> text = “true” value = “ 1”/> <listitem> text = “false” value = “ 0”/> /> </form>

Summary of cart logic n Up to three different labels for product ID: Ø

Summary of cart logic n Up to three different labels for product ID: Ø product table value Ø parameter value Ø cart value n This can be very confusing, but the cart won’t work without the right associations Ø all cart control parameters must be correctly entered… otherwise… » no data or the wrong data will be added » incorrect parameters will be invoked causing. net errors

Web. Xel. Cart Assembly n Collection of compiled versions of many coded web controls:

Web. Xel. Cart Assembly n Collection of compiled versions of many coded web controls: Ø Cart Ø Add. From. DB Ø Write. To. DB Ø etc. . n Visual Studio can “open” an assembly to reveal defined properties of each control

Web. Xel. Cart: Cart control n Like any other asp. net web control… Ø

Web. Xel. Cart: Cart control n Like any other asp. net web control… Ø stored as executable code n Visual Studio makes it available to any. net pages in the project… Ø assuming parent assembly is… » held in the relevant /App_Data folder » and added to “References” n Can be called as <control name>… < /> Ø like any other web control Ø parameters have to be set accordingly

Passing the Product ID Parameter Product ID value sent as e. g. “Prod. ID”

Passing the Product ID Parameter Product ID value sent as e. g. “Prod. ID” Product Page Remote DB Add from DB scripts Shopping Cart fields extracted from remote database

Updating Cart Values n When the customer clicks on a hyperlink/button associated with a

Updating Cart Values n When the customer clicks on a hyperlink/button associated with a product: Ø a “line record” is created on the cart for that product (based on the parameter passed) Ø quantity is set at the default value of 1 n n Cart Totals are then automatically calculated If customer clicks on a second product… Ø further cart record is created Ø all cart totals recalculated

Cart Display Page n n n Cart. aspx consists of a HTML table with

Cart Display Page n n n Cart. aspx consists of a HTML table with scripts added to insert cart values in the right places on the table Pages should be designed so cart. aspx is available at all times, so the customer can readily remind themselves what they have clicked on… Cart. aspx should also include: Ø update product quantity (e. g. buy 2 instead of 1) Ø remove a cart line altogether (erase cart record)

Rest of Shopping System Registration for new customer/Login for existing customer (personal data) n

Rest of Shopping System Registration for new customer/Login for existing customer (personal data) n Online Invoice n Payment Page (sensitive personal data) n Thank you page n Goodbye and come again page n Email updates on order progress n

Thanks for listening…

Thanks for listening…