Download presentation
Presentation is loading. Please wait.
Published byAnthony Leonard Modified over 8 years ago
1
e-Commerce: Introduction to ASP.Net Application Architecture 1 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Topics Introduction to Forms and their Contents Event Code Code-Behind Processing Forms—The Required Round Trip The Viewstate Processing Forms—Breaking Free The Page_Load Event
2
e-Commerce: Introduction to ASP.Net Application Architecture 2 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Forms and their Contents Every.ASPX page contains one and only one form Forms are objects that support events Forms contain subordinate objects –Most important is the collection of controls –May have other containers that may also contain controls (more later) –Controls also support events and have properties
3
e-Commerce: Introduction to ASP.Net Application Architecture 3 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Forms and their Contents (cont.) Certain actions on the displayed form cause the form and its controls to be posted back to the server –Pushing any button on the form –Setting the Autopostback property of a control to True (default is False) At the server, code in the relevant events executes –Page_Load event always executes (if present) –Other controls have event code that execute if the event occurred since the last page submission –We will look at events as we look at controls
4
e-Commerce: Introduction to ASP.Net Application Architecture 4 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Event Code Many objects, especially controls, have events for which code can be written Fewer events than in Windows versions Common Events include –Page (or form): Page_Load –Button: _Click –Lists: _SelectedIndexChanged –Text Box: _TextChanged Private Sub cmdSubtract_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdSubtract.Click lblAnswer.Text = (CSng(txtFirstNum.Text) _ - CSng(txtSecondNum.Text)).ToString End Sub
5
e-Commerce: Introduction to ASP.Net Application Architecture 5 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Event Code (Cont.) It is common for the same form posting to execute multiple events It is sometimes important to know the order in which the events execute –You will need to experiment when this is the case –How would you do this? Only one button _Click event should ever execute
6
e-Commerce: Introduction to ASP.Net Application Architecture 6 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Code Behind There are two possible places to put your event code –Procedures at the top of your HTML view of the page Illustrated in the majority of ASP.Net books available on the market –“Code Behind” page associated with your form Approach in our text We will always use Code Behind –Sharable –Uses same VB environment –Better editor support
7
e-Commerce: Introduction to ASP.Net Application Architecture 7 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu The Form Round Trip When a form is submitted, it posts back to itself –Code associated with the form handles all events triggered by the form and its controls –Unless directed otherwise, the form redisplays itself –This programming model defaults to keeping application on one form But most applications require a sequence of forms What are the advantages of this approach? How do we break free (move to different forms)? Classic ASP and ASP.Net 2.0 do allow forms to post to a different page (form)
8
e-Commerce: Introduction to ASP.Net Application Architecture 8 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu The Form Round Trip—Staying on the Same Page Some applications require recursive use of one page –Repeatedly adding products to a shopping cart –Registering for multiple classes Many, many applications require validation of some step before moving to the next step (form) –Failed validation needs display of the same information along with information on the problem to be corrected –Major advantage of the round trip model
9
e-Commerce: Introduction to ASP.Net Application Architecture 9 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu The Form Round Trip—Staying on the Same Page (Cont.) Form flexibility –It is possible to make one form behave like many by putting controls in panels –Panels can be selectively turned visible or invisible depending on progress through the application –A single form, therefore, can look like a series of forms executing a sequential transaction –Code can be easily used throughout the form But same thing can be used with independent classes (classes not tied to a form)
10
e-Commerce: Introduction to ASP.Net Application Architecture 10 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu The Viewstate Viewstate is a special hidden control on a form that records the state of the form when it was last sent to the browser Hidden controls are controls that are just what you’d think they are –They contain values –Are submitted to the server with the form –Are not visible to the user –Hidden controls can be seen in the View Source menu from the browser
11
e-Commerce: Introduction to ASP.Net Application Architecture 11 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu The Viewstate (Cont.) Viewstate values are encoded (but not encrypted) Viewstates can be encrypted (we will not cover) Viewstate changes with contents of the form –Viewstate on our form when it first loads –Viewstate when a different color is selected and different numbers entered in the text boxes <input type="hidden" name="__VIEWSTATE" value="dDwtMTE4NjMwMjUyNTs7Pp07RgJob7AmWNf0ELcqIroUfgAO" /> <input type="hidden" name="__VIEWSTATE" value="dDwtMTE4NjMwMjUyNTt0PDtsPGk8MT47PjtsPHQ8O2w8aTwxPjtpPD M+Oz47bDx0PDtsPGk8Mz47PjtsPHQ8cDxwPGw8VGV4dDs+O2w8Qmx1ZTs+Pj s+Ozs+Oz4+O3Q8O2w8aTw1Pjs+O2w8dDxwPHA8bDxUZXh0Oz47bDwxNTs+Pj s+Ozs+Oz4+Oz4+Oz4+Oz4zrwEswgAzGgO08kaDxfYLD3Tr+w==" />
12
e-Commerce: Introduction to ASP.Net Application Architecture 12 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu The Viewstate (Cont.) Viewstate can lead to efficiencies Controls automatically retain their contents unless changed by the user or explicitly changed in code –Old ASP required controls to be explicitly set or they defaulted to their design values Trips to the database can be avoided –If DB values are unlikely to have changed controls can be loaded from the DB on the first load –Subsequent displays can be loaded from the viewstate
13
e-Commerce: Introduction to ASP.Net Application Architecture 13 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu The Viewstate (cont.) Sometimes viewstate gets in the way –If real-time database values are required for every page load the old values are still retained in viewstate –Viewstate for large amounts of data can take up as much space as the data itself Viewstate for individual controls can be disabled by setting the EnableViewState property to False –True is the default value
14
e-Commerce: Introduction to ASP.Net Application Architecture 14 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu Processing Forms—Breaking Free Execution can be branched to another form using a variety of techniques Most common is the Response.Redirect option –Response.Redirect(“TargetUrl”) –Response.Redirect(“WebForm2.aspx”) –“Response” is an intrinsic (built in, always available) object that reflects the stream of output being sent to the browser –.Redirect changes the page that provides the response to the specified URL –Response.Redirect does not carry any values forward
15
e-Commerce: Introduction to ASP.Net Application Architecture 15 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu The Page Load Event & IsPostBack The Page_Load event is automatically provided with every code behind form page (but may be erased) The Me.IsPostBack logical property can be used to tell if … –False: Page is being loaded (for the first time) based on an external call –True: Page is being loaded because the form was posted back to itself Especially useful to determine when controls must be loaded by a database call
16
e-Commerce: Introduction to ASP.Net Application Architecture 16 Dr. Lawrence West, Management Dept., University of Central Florida lwest@bus.ucf.edu The Page Load Event & IsPostBack (cont.) Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load '************************************************************ '* Tests to see if the page is posting back and sets a label '* value depending on the status '************************************************************ If Me.IsPostBack Then lblIsPostBack.Text = " has posted back." Else lblIsPostBack.Text = " has not posted back." End If End Sub If Not Me.IsPostBack Then Common test for initial page load
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.