OOP Class9
Contents
Anatomy of an aspx page
Page Directive The page directive must be on the first line of an aspx page. It consists of Name/Value Pairs and sets parameters that will be used throughout the execution of the page ie. the language.
<%@ Page language="c#" debug="True" trace="False"%>
C# Code may be embedded in the page using script tags similar to javascript
<script language="c#" runat="server">
</script>
Notice the runat attribute is set to 'server'. This is what makes the code execute on the server rather than be parsed by the client.
Console applications start executing in the main method
<csharp>public static void Main() {}</csharp>
The .Net Framework can also execute on the web. Rather than having a Main method a web page starts it's execution with a method called Page_Load
<csharp>public Page_Load { }</csharp>
There are actually several method that are executed in an aspx page.
http://samples.gotdotnet.com/quickstart/aspplus/
Simple Aspx Page
http://iam.colum.edu/oop/classsource/class8/Aspx/hello.aspx hello.aspx
http://iam.colum.edu/oop/classsource/class8/Aspx/hello2.aspx hello2.aspx
http://iam.colum.edu/oop/classsource/class8/Aspx/Label.aspx Label.aspx
In class Build three hello aspx pages similar to the ones above
Dogs on the web
HTML and HTTP
Http is a stateless protocol. There is mo mechanism built in to the protocol that allows the server to remeber clients or requests. An http simply responds to http verbs GET, POST, PUT, DEL, TRACE etc. contained in RFC 2068 HTTP/1.1
regular html forms post information using forms in 2 ways with a get or a post http request.
Get
- Get send information to the server using the URI. Limited to 1024 character in some browsers and servers.
Example http://iam.colum.edu/oop/classsource/class9/simpleGet.html simpleGet.html - source
The simpleGet.html pages form has the action of 'simpleGet.aspx' this mean that when the form is submitted the browser will request the 'simpleGet.aspx' with whatever parameters are in the form. Since the method = get these pararmeters will show up are query string parameters
URI and querystring parameters
URI - Universal Resource Identifier http://src.doc.ic.ac.uk/computing/internet/rfc/rfc1630.txt RFC1630
Http URI
Protcol | Host | Port | Path | File | Fragment identifier | Querystring |
---|---|---|---|---|---|---|
http:// | info.cern.ch | :8000 | /imaginary/test/ | file.html | #link | ?test=yes |
Post
- Post posts the variables in the HTTP Header.
Example http://iam.colum.edu/oop/classsource/class9/simplePost.html simplePost.html - source
FirstName: LastName: Header Name Value HttpMethod GET Connection keep-alive Keep-Alive 300 Accept text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Accept-Encoding gzip,deflate Accept-Language en-us,en;q=0.5 Cookie ASP.NET_SessionId=ezfgw255ix0zd5yogj3eawej Host imdev Referer http://imdev/infod/jeff/ User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1
REST
- Representational state transfer
https://www.ibm.com/developerworks/webservices/library/ws-restful/
http://www.infoq.com/articles/rest-introduction
http://iam.colum.edu/oop/classsource/class9/htmlForms.html htmlForms.html - source
Courses on the Web
Response Object
Response.Write()
http://iam.colum.edu/oop/classsource/class10/Response/Response1.aspx Response1.aspx - source
Response.End()
http://iam.colum.edu/oop/classsource/class10/Response/Response2.aspx Response2.aspx - source
Response.Clear()
http://iam.colum.edu/oop/classsource/class10/Response/ResponseClear.aspx ResponseClear.aspx - source
Response.Flush()
http://iam.colum.edu/oop/classsource/class10/Response/ResponseFlush.aspx ResponseFlush.aspx - source
Debugging with response object
Using the response buffer can be extreemely usefull for debugging. Sometime you may have to setup a small debug system to help catch errors in object that do not inherit from System.UI.Page.
Response.Flush()
http://iam.colum.edu/oop/classsource/class10/Response/ResponseDebug.aspx ResponseDebug.aspx - source
Home Work
Convert one of your classes to work as an aspx page.
Have a nice break....
Interfaces
Interfaces - implements or derives from
An interface is a class that lacks implementation. The only thing it contains are definitions of events, indexers, methods and/or properties. The reason interfaces only provide definitions is because they are inherited by classes and structs, which must provide an implementation for each interface member defined. Since classes in c# can only be derived from one other class ( in c++ it is possible to derive from many) interfaces are used for multiple inheritance. Like abstract classes you cannot create an instance of an interface on derive from it.
You cannot create an instance of an interface.
Interfaces cannot contain any implementation.
Interfaces are basically a contract between classes. When a class implements an interface it is promising to implement the propery and method signature of that interface. This helps with abstraction and encurages polymorphism.
In order to specify that a class implements an inteface you use a : after the class name:
<csharp>public class NewDog : IWalkable</csharp>
This means that the NewDog class implements the IWalkable interface.
Interfaces support multple inheritance.
<csharp>public class NewDog : IWalkable,IBark</csharp>
Now the NewDog Class promises to implement the IWalkable and the I Bark interface.
Phone interface example with single inhertance
phoneIFace.cs -source
Multiple interface inheritance - inherits IPhone, Cell, POTS phoneIFacePOTS.cs -source
Phone Interface UML
IComparable and Polymorphism
In order to allow build in Arry type like the ArrayList to be able to Sort and Reverse your classes need to implement the IComparable Interface. System.IComparable on MSDN
After your classes implement this interface it will allow for polymorphic method like Sort and Reverse to work.
Here is an exmaple of a dog array that will not sort
This IComplare Interface has calls for a methpod called CompareTo. The CompareTo method shoudl retun an positive integerg if that the current object is grater that the object that is being compared. It should oreturn 0 is they are equal. And it should return a negative integer is the current object is less than the compared object.
The UML for a dog that implemets IComparable
Now here is an example of a Dog class that implements the IComparable inteface.
Other Resources on Interfaces
Web Forms
Examples of Web Forms
http://iam.colum.edu/oop/classsource/class9/aspForms.aspx aspForms.aspx - Source
todo examples of all the html elements and how they react to form submits
Persisting Data
POST and GET
an asps page posting to itself
Events in c# on the web
Event handlers
OnClick
http://iam.colum.edu/oop/classsource/class9/events/events1.aspx events1.aspx - source
OnCommand
http://iam.colum.edu/oop/classsource/class9/events/events2.aspx events2.aspx - source
http://iam.colum.edu/oop/classsource/class9/events/events3.aspx events3.aspx - source
Home Work
Make an post an catch aspx page with a form of your very own that asks questions about one of your objects. Use regular html form and aspx events to demostrate you class.
Use the form values to create a new instance of your class.
Get and read Intro and Chapter 1 of Head Start Design Patterns