220 likes | 239 Views
Learn the basics of object-oriented programming in ASP.NET, including the concept of objects, properties, methods, and events. Explore other important object concepts and understand how to use Request and Response objects in ASP.NET.
E N D
CIS 451: ASP.NET Objects Dr. Ralph D. Westfall January, 2009
Object Oriented Programming • many popular newer programming languages are object-oriented • C++, Java, JavaScript • Visual Basic.NET is a true object-oriented (OO) language • in contrast to VB 6 (which however did have objects with very useful properties)
What Is an Object? • 3 types of things grouped together • properties (like variables: descriptive) • like a "super" variable (cf. hours PM) • methods (like functions: actions) • events (things that it responds to) • similarities with real world objects • car (properties? methods? events?) • dog " "
Object Features • properties: name/value pairs • age = 21; height = 66"; weight = 135#; • methods: code that does something • calculations, write to screen, send e-mail • may have either/both of following: • parameters (inputs) • return values (outputs)
Object Features - 2 • events: things happen, object responds to them • like a method, but triggered externally • user clicks a button, or time runs out • can have parameters and/or return values • asynchronous: can occur at any time • rather than scheduled, or at same time as something else (synchronous)
Other Object Concepts • class = template or general model • instance of class has specific attributes • telephone = class, my phone = instance • encapsulation: details of object hidden • inside a "black box" • only need to know the interface: how to call the methods and what the inputs and/or outputs are
Other Object Concepts - 2 • "dot" notation identifies attributes and methods of objects • Request.TotalBytes • Request.QueryString.Count • Count (of arguments) in QueryString is a property of Request Object • Response.Write() • Write is a method of Response Object
ASP Core Objects • Request: messages from caller (e.g., web page or process) • Response: messages to caller (browser) • Server: variables and utility functions • Application: data storage in memory for all web pages in the application • Session: identifies individual user interactions
Request Object • html or another asp file requests an asp file • Request Object has information from page that requested the asp file • information is stored in "collections" • contain pairs of variable names and their values (name/value pairs) • data values can be accessed by item name • Request.QueryString("page")
Request Object Data • "get" method in HTML calling page • <form action="buya.aspx" method=get> • "get" appends data to URL (URL encoding) • limit around 1000 characters (256 – 2000+) • data available in Request.QueryString "collection," indexed by name of form element • Request.QueryString("page") • form has textbox, textarea, checkbox, radio button, or etc. element with name="page"
URL Encoding • ? after URL shows start of data • pairs of: name=value • & (ampersands) between pairs • + between separate words (= space) • special characters coded as %nn (+ is %2B) • http://www.company.com/search.asp? • …?cust=Jabbar&address=231+10th+St • example: try doing a Yahoo search
Request Object Data - 2 • "post" method in HTML calling page • <form action="buya.aspx" method=post> • sends data by a separate route • size is essentially unlimited • data available through Request.Form "collection," indexed by name of form element • Request.Form("prod1") • form has textbox, textarea, checkbox, radio button, or etc. element with name="prod1"
Get versus Post • get (Request.QueryString) [object.property]) • better for debugging (can look at query string in URL box on browser) • post (uses Request.Form) • can handle more data • safer (less susceptible to hacking) • always use post for database updates • easy to convert Request.QueryString to Request.Form with global search/replace
Using QueryString in an aspx File • can create the same effect sending from an aspx file as from a html file Response.Redirect("rsp.aspx?Name=" & _ this.txtName.Text & "&LastName=" & _ this.txtLastName.Text) • values are URL encoded and visible in browser address of destination page • one way to migrate from html to aspx
Response Object • sends information to client • can control when and how to send it • can control caching/expiration of page • can send parts or all of a web page to client
Response Methods • Response.Write(x1 [& " " & x2 [& ...]]) • x1 etc. can be anything printable (text, #s, etc., or functions that return printable characters) • .NET has parentheses hold argument(s) • <%= x1 [& " " & x2 [& " " & etc.]%> • same effect as Response.Write, in less space • less efficient (don't use <%= a lot)
Response Methods - 2 • redirecting to another page Response.Redirect("buya.aspx") • stops processing on current page and goes to the other page • can go to external pages too
Server Transfer Method • can also use to redirect to another page, but only if it's on same server Server.Transfer("buya.aspx") • doesn't change URL in browser address • comparison of Response.Redirect and Server.Transfer
Exercise: Form Processing • use Dreamweaver to create a form that uses 8+ form elements/variations in its Windows>Objects>Forms toolbar • set name property for each element • select options (e.g., text input can be single or multiline, or password) • see HTML Form Reference for syntax • save file as a .html file
Exercise: Form Processing - 2 • use Visual Studio to • create a project • add the .html file you created on the previous page to the project • create a .aspx file with Response.Write statements to print inputs/selections from each input element from the .html file • test it on your workstation or computer
Multiple Selection Data • can allow multiple selections in a list box on a form • <select size=2 name="loc" multiple> • <option>Paris</option > • <option>Sydney</option > • <option>Buenos Aires</option > • </select> • may send multiple items with same name ("loc" in this example)
Multiple Selection Data - 2 • For intI = 1 to _ • Request.Form("loc").Count • Response.Write _ • (Request.Form("loc")(intI)) • Next • note dot connecting Form object's ("loc") item to Count