Difference between revisions of "OOP Class10"
(→Session State) |
|||
(21 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
+ | [[Category:IAM Classes]] | ||
+ | |||
+ | =GO to ANDROID BASICS PAGE= | ||
+ | |||
+ | |||
+ | '''[[Basic Android]]''' | ||
+ | |||
+ | |||
+ | ==About aspx== | ||
+ | |||
+ | [[VS Web Projects]] | ||
+ | |||
+ | {{Anatomy of an aspx page}} | ||
+ | |||
+ | ==Simple Aspx Page== | ||
+ | |||
+ | http://iam.colum.edu/oop/classsource/class8/Aspx/hello.aspx [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class8/Aspx/hello.aspx hello.aspx] | ||
+ | |||
+ | http://iam.colum.edu/oop/classsource/class8/Aspx/hello2.aspx | ||
+ | [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class8/Aspx/hello2.aspx hello2.aspx] | ||
+ | |||
+ | http://iam.colum.edu/oop/classsource/class8/Aspx/Label.aspx | ||
+ | [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class8/Aspx/Label.aspx Label.aspx] | ||
+ | |||
+ | In class | ||
+ | Build three hello aspx pages similar to the ones above | ||
+ | |||
+ | ==Dogs on the web== | ||
+ | [http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class8/DogWeb DogWeb] | ||
+ | |||
+ | |||
+ | |||
+ | {{HTML and HTTP}} | ||
+ | |||
+ | |||
+ | http://iam.colum.edu/oop/classsource/class9/htmlForms.html [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class9/htmlForms.html htmlForms.html - source] | ||
+ | |||
+ | ==Courses on the Web== | ||
+ | [http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class8/StudentWeb Student Web] | ||
+ | |||
+ | ==Response Object== | ||
+ | |||
+ | Response.Write() | ||
+ | |||
+ | http://iam.colum.edu/oop/classsource/class10/Response/Response1.aspx | ||
+ | [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class10/Response/Response1.aspx Response1.aspx - source] | ||
+ | |||
+ | Response.End() | ||
+ | |||
+ | http://iam.colum.edu/oop/classsource/class10/Response/Response2.aspx | ||
+ | [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class10/Response/Response2.aspx Response2.aspx - source] | ||
+ | |||
+ | Response.Clear() | ||
+ | |||
+ | http://iam.colum.edu/oop/classsource/class10/Response/ResponseClear.aspx | ||
+ | [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class10/Response/ResponseClear.aspx ResponseClear.aspx - source] | ||
+ | |||
+ | Response.Flush() | ||
+ | |||
+ | http://iam.colum.edu/oop/classsource/class10/Response/ResponseFlush.aspx | ||
+ | [http://iam.colum.edu/oop/gbrowser.php?file=/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 | ||
+ | [http://iam.colum.edu/oop/gbrowser.php?file=/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.... | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ==Web Forms== | ||
+ | Examples of Web Forms | ||
+ | |||
+ | http://iam.colum.edu/oop/classsource/class9/aspForms.aspx [http://iam.colum.edu/oop/gbrowser.php?file=/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=== | ||
+ | |||
+ | http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class9 | ||
+ | |||
+ | an asps page posting to itself | ||
+ | |||
+ | '''GET''' | ||
+ | |||
+ | Simple get page | ||
+ | http://iam.colum.edu/oop/classsource/class9/simpleGet.html [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class9/simpleGet.html source] | ||
+ | |||
+ | http://iam.colum.edu/oop/classsource/class9/simpleGet.aspx [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class9/simpleGet.aspx source] | ||
+ | |||
+ | '''POST''' | ||
+ | |||
+ | Simple post page http://iam.colum.edu/oop/classsource/class9/simplePost.html [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class9/simplePost.html source] | ||
+ | |||
+ | http://iam.colum.edu/oop/classsource/class9/simplePost.aspx [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class9/simplePost.aspx source] | ||
+ | |||
+ | Simple post page http://iam.colum.edu/oop/classsource/class9/simplePostValues.html [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class9/simplePostValues.html source] | ||
+ | |||
+ | http://iam.colum.edu/oop/classsource/class9/simplePostValues.aspx [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class9/simplePostValues.aspx source] | ||
+ | |||
+ | |||
+ | '''ASP POSTBACK''' | ||
+ | |||
+ | http://iam.colum.edu/oop/classsource/class9/postBack.aspx [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class9/postBack.aspx source] | ||
+ | |||
+ | {{ASPX csharp commands}} | ||
+ | |||
+ | ==Home Work== | ||
+ | |||
+ | Create a web page with a button that uses your class. | ||
+ | |||
+ | |||
+ | |||
+ | |||
==Response Object== | ==Response Object== | ||
Line 46: | Line 173: | ||
[http://iam.colum.edu/oop/gbrowser.php?file=/global.asax global.asax-source] | [http://iam.colum.edu/oop/gbrowser.php?file=/global.asax global.asax-source] | ||
− | http:// | + | http://iam.colum.edu/oop/classSource/class10/ApplicationCnt.aspx - source |
GlobalAsax on c-sharp corner | GlobalAsax on c-sharp corner | ||
Session - The web server creates a session for each unique visitor | Session - The web server creates a session for each unique visitor | ||
+ | |||
+ | |||
+ | http://digg.com/programming/Web_3_0_Amazing_button_developed_by_Columbia_College_IAM_Department_Head | ||
+ | |||
+ | ==Dog and Dog Binding== | ||
+ | |||
+ | DataBound controls can bind to anyting that implements [http://msdn2.microsoft.com/en-us/library/system.collections.ienumerable.aspx IEnumerable] | ||
+ | It's easier in 2.0 + to just inherit from [http://msdn2.microsoft.com/en-us/library/system.collections.icollection.aspx ICollection] | ||
+ | |||
+ | http://iam.colum.edu/oop/classsource/class9/DogBinding.aspx [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class9/DogBinding.aspx source] [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class9/DogBinding.aspx.cs codebehind] | ||
+ | |||
+ | TODO in class Bark a Dog... | ||
+ | |||
+ | Special Files | ||
+ | *web.config | ||
+ | |||
+ | Special Folders | ||
+ | *App_Code | ||
+ | *Bin | ||
+ | *App_Data | ||
==Session State== | ==Session State== | ||
− | Session is an array of objects that is help on the server for each unique user. Each time a browser requests an aspx page the sever sends back a unique id as a cookie called SESSIONID. This cookie is used to | + | Session is an array of objects that is help on the server for each unique user. Each time a browser requests an aspx page the sever sends back a unique id as a cookie called SESSIONID. This cookie is used to retrieve each browsers session array on each request. |
− | Boxing and unboxing of session | + | Boxing and unboxing of session variables |
− | All Session variables are of type object. This means that you can put | + | All Session variables are of type object. This means that you can put whatever you like into the session array. The trouble starts when you remove and object from the array it will always be removed and be of type object. You then need to cast the object into its actual data type. This is known as boxing and unboxing. |
− | <csharp>Session["Counter"] = 1; //set the session variable Counter equal to the integer 1 the int is boxed as an object | + | <syntaxhighlight lang="csharp">Session["Counter"] = 1; //set the session variable Counter equal to the integer 1 the int is boxed as an object |
− | int i = (int)Session["Counter"] //cast the object Session["Counter"] bact to an int unboxing</ | + | int i = (int)Session["Counter"] //cast the object Session["Counter"] bact to an int unboxing</syntaxhighlight> |
exmaple with boxing and unboxing of ints | exmaple with boxing and unboxing of ints | ||
Line 96: | Line 243: | ||
http://iam.colum.edu/oop/classsource/class10/events/DogDelegateEvent.aspx | http://iam.colum.edu/oop/classsource/class10/events/DogDelegateEvent.aspx | ||
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class10/events/DogDelegateEvent.aspx DogDelegateEvent.aspx - source] | [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class10/events/DogDelegateEvent.aspx DogDelegateEvent.aspx - source] | ||
+ | |||
+ | http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class10/events | ||
==Home Work== | ==Home Work== | ||
− | + | Do the windows form from last week. | |
− | + | Use you class and dsiplay daya in a control like we did today. | |
+ | |||
+ | Do the same thing with class but use controls on the web. |
Latest revision as of 16:31, 10 June 2019
Contents
- 1 GO to ANDROID BASICS PAGE
- 1.1 About aspx
- 1.2 Anatomy of an aspx page
- 1.3 Simple Aspx Page
- 1.4 Dogs on the web
- 1.5 HTML and HTTP
- 1.6 Courses on the Web
- 1.7 Response Object
- 1.8 Home Work
- 1.9 Web Forms
- 1.10 Persisting Data
- 1.11 Home Work
- 1.12 Response Object
- 1.13 Application State
- 1.14 Dog and Dog Binding
- 1.15 Session State
- 1.16 View State
- 1.17 More Event Samples
- 1.18 Home Work
GO to ANDROID BASICS PAGE
About aspx
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....
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
http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class9
an asps page posting to itself
GET
Simple get page http://iam.colum.edu/oop/classsource/class9/simpleGet.html source
http://iam.colum.edu/oop/classsource/class9/simpleGet.aspx source
POST
Simple post page http://iam.colum.edu/oop/classsource/class9/simplePost.html source
http://iam.colum.edu/oop/classsource/class9/simplePost.aspx source
Simple post page http://iam.colum.edu/oop/classsource/class9/simplePostValues.html source
http://iam.colum.edu/oop/classsource/class9/simplePostValues.aspx source
ASP POSTBACK
http://iam.colum.edu/oop/classsource/class9/postBack.aspx source
Events in c# and aspx
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
<csharp>
<%@ Page language="c#" debug="True" trace="false"%>
<script language="c#" runat="server">
Dog fido; public void Page_Load() { fido = new Dog(); }
public void Button1_OnClick(Object sender, EventArgs e) { Response.Write("Hello from Button1_OnClick"); lblBark.Text = fido.Bark(); }
public class Dog { public string Name; // the dog's name public int Age; // the dog's age public int Weight; // the dog's weight public string BarkSound; // the sound of the dog's bark
public Dog() { BarkSound = "Woof!!!"; }
public string Bark() { return this.BarkSound; } public void Eat() { //put eat code here } }
</script>
Home Work
Create a web page with a button that uses your class.
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
Application State
Application - application level variables have application scope meaning they are avialible to the entire application.
example
http://iam.colum.edu/oop/classsource/class10/application/ApplicationSet.aspx ApplicationSet.aspx - source
http://iam.colum.edu/oop/classsource/class10/application/ApplicationSetUpdateFixed.aspx ApplicationSetUpdateFixed.aspx - source
file global.asax global.asax-source
http://iam.colum.edu/oop/classSource/class10/ApplicationCnt.aspx - source
GlobalAsax on c-sharp corner Session - The web server creates a session for each unique visitor
http://digg.com/programming/Web_3_0_Amazing_button_developed_by_Columbia_College_IAM_Department_Head
Dog and Dog Binding
DataBound controls can bind to anyting that implements IEnumerable It's easier in 2.0 + to just inherit from ICollection
http://iam.colum.edu/oop/classsource/class9/DogBinding.aspx source codebehind
TODO in class Bark a Dog...
Special Files
- web.config
Special Folders
- App_Code
- Bin
- App_Data
Session State
Session is an array of objects that is help on the server for each unique user. Each time a browser requests an aspx page the sever sends back a unique id as a cookie called SESSIONID. This cookie is used to retrieve each browsers session array on each request.
Boxing and unboxing of session variables
All Session variables are of type object. This means that you can put whatever you like into the session array. The trouble starts when you remove and object from the array it will always be removed and be of type object. You then need to cast the object into its actual data type. This is known as boxing and unboxing.
Session["Counter"] = 1; //set the session variable Counter equal to the integer 1 the int is boxed as an object
int i = (int)Session["Counter"] //cast the object Session["Counter"] bact to an int unboxing
exmaple with boxing and unboxing of ints
http://iam.colum.edu/oop/classsource/class10/SessionCnt.aspx SessionCnt.aspx - source
Dog examples
The first page creates a dog objects and stores it as a session variable.
http://iam.colum.edu/oop/classsource/class10/dogsession/dog.aspx dog.aspx - source
http://iam.colum.edu/oop/classsource/class10/dogsession/dog_session_Page1.aspx dog_session_Page1.aspx - source
http://iam.colum.edu/oop/classsource/class10/dogsession/dog_session_Page2.aspx dog_session_Page2.aspx - source
http://iam.colum.edu/oop/classsource/class10/dogsession/dog_session_Page3.aspx dog_session_Page3.aspx - source
View State
ViewState is an instance of statebag todo demostate viewstate
look at guessing game
http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class10/assign3
More Event Samples
A nice exmaple of a web page that uses exents to create and use our dog class. Notice hot the events and the dog class are only loosely coupled (the doesn't directly use the dog class).
http://iam.colum.edu/oop/classsource/class10/events/DogDelegateEvent.aspx DogDelegateEvent.aspx - source
http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class10/events
Home Work
Do the windows form from last week. Use you class and dsiplay daya in a control like we did today.
Do the same thing with class but use controls on the web.