Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP3241 E-Business Technologies Richard Henson University of Worcester October 2012.

Similar presentations


Presentation on theme: "COMP3241 E-Business Technologies Richard Henson University of Worcester October 2012."— Presentation transcript:

1 COMP3241 E-Business Technologies Richard Henson University of Worcester October 2012

2 Week 6: Coding a C# Shopping Cart n By the end of this session you will be able to:  explain the structure of a C#.aspx page including the code-behind file & the design file  apply principles of asp.net design to the creation of a shopping system

3 C# Classes n n A class is a bit like an entity in systems analysis…   can have “properties”   equivalent to attributes… n n Has to be carefully coded…   could be “public” or “private”   properties are variables » »should really be “private”   code snippets (methods) – public

4 C# Properties n n Required to “instantiate” a class   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

5 C# Constructors & Methods n n These are different [public] code segments associated with a class   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

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

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

8 “ Classes” and the code behind model n the.net framework also sets up the JIT run-time environment, and converts source code to “intermediate language” (IL)

9 “classes” and the code behind model n This means that the code that runs is not solely the code that was created for the.aspx page and its associated “code behind” (!)

10 Generating and Running the Page’s “class” Code 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!

11 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

12 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.

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

14 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

15 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…

16 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

17 Using C# classes with “event- driven” controls EXAMPLE (an “add to cart” button): protected void DataList1_ItemCommand (objectsource, DataListCommandEventArgs e) { if (e.CommandName == "AddToCart") { // Add code here to add the item to the shopping cart. // Use the value of e.Item.ItemIndex to retrieve the data // item in the control. }}

18 The “global.asax” n An optional way of including otherwise unlisted events, objects, with application aspx scripts  normally placed in root folder n.NET is configured so that any direct URL request for global.asax is automatically rejected »external users therefore cannot download or view the code in it »makes the settings very secure… n Parsed & compiled at run time  dynamically generated.NET class (HttpApplication)

19 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 tag and its LANGUAGE added within the.aspx code »usually placed after the tags »RUNAT attributes (where code is executed) optional  Syntax: » » n script… » » »similar to embedded client code

20 Keep it simple! n 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

21 Potential Scripting Problems in a RAD Environment… 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

22 Potential Problems of Scripting in the RAD Environment… (continued) 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…

23 Use of Comments in aspx files n Apostrophe for VBScript  ‘ This is a VBScript comment Three slashes for C# Three slashes for C#  ///  ///  /// The “myClass” class represents an arbitrary class  ///  /// n An excellent C# resource:  http://www.softsteel.co.uk/tutorials/cSharp/cont ents.html http://www.softsteel.co.uk/tutorials/cSharp/cont ents.html http://www.softsteel.co.uk/tutorials/cSharp/cont ents.html

24 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

25 “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

26 ASP. Net Components n Early IIS ASP environment was mostly COM components!  Problem having a webserver containing a lot of source code! n Microsoft still provide a library of COM components  Many old asp components in VB that have been rewritten and converted into C# assemblies

27 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://msdn2.microsoft.com/en-us/library/bb735856.aspx http://msdn2.microsoft.com/en-us/library/bb735856.aspx

28 Evolution of “VBScript Request” object  “Request” was the only way asp scripts could retrieve the input data entered by the user in the input fields in the page  used to access information passed from the browser when requesting a Web page  handles HTML form fields, client certificate information, and cookies

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


Download ppt "COMP3241 E-Business Technologies Richard Henson University of Worcester October 2012."

Similar presentations


Ads by Google