5/6/2019 Session 8.2 Postback, ViewState

Slides:



Advertisements
Similar presentations
Unit 02. ASP.NET Introduction HTML & Server controls Postbacks Page Lifecycle.
Advertisements

11 Getting Started with ASP.NET Beginning ASP.NET 4.0 in C# 2010 Chapters 5 and 6.
JavaScript Forms Form Validation Cookies CGI Programs.
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
11 ASP.NET Controls Beginning ASP.NET 4.0 in C# 2010 Chapter 6.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
ASP.NET Programming with C# and SQL Server First Edition
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College Lecture 8: WebForms — Web-based.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
JavaScript Form Validation
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
JavaScript & jQuery the missing manual Chapter 11
HTML, GUI, ASP.NET Rina Zviel-Girshin Lecture 2
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
Beginning Web Site Development Module 1 – Dynamic Web Site Development Fundamentals of building dynamic Web sites with ASP.NET 2.0 and C# Version.
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
© Minder Chen, ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML.
Chapter 4 – Working with ASP.NET Controls Dr. Stephanos Mavromoustakos.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
CSCI 6962: Server-side Design and Programming Introduction to Active Server Pages.
Internet Technologies and Web Application Web Services With ASP.NET Tutorial: Introduction to.
Murach’s ASP.NET 4.0/VB, C1© 2006, Mike Murach & Associates, Inc.Slide 1.
1 CS 3870/CS 5870 Note04 Session Variables and Post Back.
Introduction to ASP.NET1. 2 Web applications in general Web applications are divided into two parts –The server part –The client part The server part.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Unit 3: Adding Code to a Microsoft ASP.NET Web Form.
Christopher M. Pascucci Basic Structural Concepts of.NET Managing State & Scope.
Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level.
Dynamic Dropdown Lists 1. Objectives You will be able to Use Dropdown Lists to solicit multiple choice user input in an ASPX web page. Populate a Dropdown.
Module 4: Creating a Microsoft ASP.NET Web Form. Overview Creating Web Forms Using Server Controls.
HTML Form Widgets. Review: HTML Forms HTML forms are used to create web pages that accept user input Forms allow the user to communicate information back.
ASP.Net, Web Forms and Web Controls 1 Outline Session Tracking Cookies Session Tracking with HttpSessionState.
The Problem of State. We will look at… Sometimes web development is just plain weird! Internet / World Wide Web Aspects of their operation The role of.
The Module Road Map Assignment 1 Road Map We will look at… Internet / World Wide Web Aspects of their operation The role of clients and servers ASPX.
ASP.NET 4 Unleashed Chapter 1. .aspx page: contains C# script and HTML code including tags. Listing 1.1 FirstPage.aspx.
SYST Web Technologies SYST Web Technologies XHTML Forms.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Module 4: Creating a Web Application with Web Forms
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
11 ASP.NET Server Controls Beginning ASP.NET in C# and VB Chapter 4.
1 HTML Forms. Objectives You will be able to: Compose HTML forms, to accept input from the user. Identify the different kinds of input tags that can appear.
11 Getting Started with ASP.NET Beginning ASP.NET in C# and VB Chapters 1 and 2.
1111 Creating HTML Programatically Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML.
PostBack  When an initial request for a page (a Web Form) is received by ASP.NET, it locates and loads the requested Web Form (and if necessary compiles.
11 User Controls Beginning ASP.NET in C# and VB Chapter 8.
Beginning ASP.NET in C# and VB Chapter 9
Visual Basic.NET BASICS Lesson 9 Nested If Statements and Radio Buttons.
HTML Structure II (Form) WEEK 2.2. Contents Table Form.
ASP.NET Part 2 Instructor: Charles Moen CSCI/CINF 4230.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
E-Commerce: Introduction to ASP.Net Application Architecture 1 Dr. Lawrence West, Management Dept., University of Central Florida Topics.
Chapter 1 Getting Started with ASP.NET Objectives Why ASP? To get familiar with our IDE (Integrated Development Environment ), Visual Studio. Understand.
Computing with C# and the .NET Framework
ASP.NET Forms.
Working with ASP.NET Server Controls
Allowing File Uploads.
Session Variables and Post Back
Internet Programming Chapter 9: State Management in ASP.NET
Chapter 8 User Controls.
Introduction to Client-Server Programming
Web Programming– UFCFB Lecture 17
Unit 27 - Web Server Scripting
12/9/2018 Session 9.1 Calendar and Validation Controls Using Sessions
Module 10: Creating a Web Application with Web Forms
ASP.NET.
PROG Advanced Web Apps 2/24/2019 Session 9.2 Data Binding Wendi Jollymore, ACES.
MIS 3200 – Unit 6.1 Moving between pages by redirecting
Allowing File Uploads.
Presentation transcript:

PROG11044 - Advanced Web Apps 5/6/2019 Session 8.2 Postback, ViewState How ASP.NET pages are processed Wendi Jollymore, ACES

Review Web site projects ASP.NET pages have .ASPX extension Each project has its own directory Examples: C:/prog11044/projects/mySite C:/prog11044/projects/assign1 In Visual Studio 2008, “Web Application” Projects are more complex “Web Sites” If given the option to open a project, choose “Web Site” 5/6/2019 Wendi Jollymore, ACES

Review Server controls <asp:Label ID=“lblName” runat=“server”>Name:</asp:Label> How ASP.NET controls are rendered in HTML form E.g. ListBox and ListItem to Select and Option Common properties of controls 5/6/2019 Wendi Jollymore, ACES

Postback When a page is first requested, it is retrieved from the server and sent to the browser Then the user triggers an event E.g. clicks a button Another request is made to process this event and return an updated version of the page This is called POSTBACK 5/6/2019 Wendi Jollymore, ACES

Postback Buttons always trigger a postback Some other controls can trigger a postback They don’t by default You can change the AutoPostback property to True E.g. if you set a check box’s AutoPostback to true, it will cause a postback when clicked You can then add an event handler to it! 5/6/2019 Wendi Jollymore, ACES

AutoPostback Example Redo Exercise 3 from ASP.NET:Introduction / Exercises Delete the button control Modify the program so that the colours change when a radio button is selected 5/6/2019 Wendi Jollymore, ACES

Postback and State Recall: When you view source of page in browser Hidden field called __VIEWSTATE This retains state information about controls between postbacks Why? Because HTTP is a stateless protocol! Once your page leaves the server, it is forgotten by the server! 5/6/2019 Wendi Jollymore, ACES

Postback and State - Example Make this: (names in the notes) 5/6/2019 Wendi Jollymore, ACES

Postback and State - Example cmdGetName_Click() event: if (!txtName.Text.Equals("")) lblDisplayName.Text = "Hello, " + txtName.Text; 5/6/2019 Wendi Jollymore, ACES

Postback and State - Example cmdGetNum_Click() event: int n = 0; if (int.TryParse(txtNumber.Text, out n)) { if (n == 7) lblDisplay.Text = "You guessed my number!"; else lblDisplay.Text = "You guessed wrong."; } else { lblDisplay.Text = "I don't understand " + txtNumber.Text; } 5/6/2019 Wendi Jollymore, ACES

Postback and State - Example What happens when page loads: Browser requests stuff.aspx from server Stuff.aspx located Stuff.aspx is sent to ASPNET_ISAPI.DLL for processing Any initial scripts/code executed Special ASP.NET tags rendered in HTML Text/HTML from a. and b. are put together in single HTML stream HTML stream sent to browser 5/6/2019 Wendi Jollymore, ACES

Postback and State - Example Type a “Kaluha” in txtName and click cmdGetName: Browser sends form data with request for stuff.aspx to server Stuff.aspx located Stuff.aspx is sent to ASPNET_ISAPI.DLL for processing Any initial scripts/code executed Special ASP.NET tags rendered in HTML From scratch!! Controls created with defaults! cmdGetName event handler executed and HTML updated accordingly HTML stream sent to browser 5/6/2019 Wendi Jollymore, ACES

Postback and State - Example Now type 3 in the number box and press cmdGetNum button Browser sends form data with request for stuff.aspx to server Stuff.aspx located and sent to ASPNET_ISAPI.DLL for processing Any initial scripts/code executed Special ASP.NET tags rendered in HTML From scratch!! Controls created with defaults! cmdGetNum event handler executed and HTML updated accordingly HTML stream sent to browser 5/6/2019 Wendi Jollymore, ACES

Postback and State - Example Wait!! On second request, lblDisplayName Text property was changed Default value was “Waiting for your name..” New value was “Hello, Kaluha” On third request, stuff.aspx was created from scratch with default values lblDisplayName.Text back to “Waiting…” On this visit, we only updated lblDisplayGuess! 5/6/2019 Wendi Jollymore, ACES

Postback and State - Example Try it – what actually happens? Both labels have the values from their buttons’ event handlers How does this happen? ViewState!! Before page is finished rendering, ViewState is updated with any state changes to controls 5/6/2019 Wendi Jollymore, ACES

Postback and State - Example How it works: On second visit to server: ViewState updated: lblDisplayName Text contains “Hello, Kaluha” Before cmdGetNum event handler ViewState is examined and all controls updated accordingly After cmdGetNum event handler ViewState updated: lblDisplayGuess Text contains “You guessed wrong.” 5/6/2019 Wendi Jollymore, ACES

Postback and State - Example Some controls remember their own state information Text boxes, list boxes, radio buttons Controls that have a corresponding HTML tag <input type=“text” value=“Kaluha” /> <input type=“radio” value=“blue” checked=“true” /> State of these controls is part of form data (method=“POST”) 5/6/2019 Wendi Jollymore, ACES

Summary: How Pages are Processed Browser sends request for aspx page to server. Server retrieves the page and sends page to ASPNET_ISAPI.DLL ASPX page is constructed and rendered in plain HTML, all controls being given their default values. Initial scripts/events executed and HTML updated accordingly. Newly rendered HTML is sent to browser User triggers postback (e.g. button click) Browser sends form data and request for aspx page to server Continued…. 5/6/2019 Wendi Jollymore, ACES

Summary: How Pages are Processed Server retrieves the page and sends page to ASPNET_ISAPI.DLL ASPX page is constructed and rendered in plain HTML, all controls being given their default values. Initial scripts/events executed and HTML updated accordingly. ViewState field is examined and controls updated Requested event handler is executed and HTML updated accordingly ViewState fields is updated to reflect any changes made to relevant controls. The stream of HTML is sent to the browser and displayed. 5/6/2019 Wendi Jollymore, ACES

More on ViewState You can also use ViewState for “global” variables You can’t have normal globals on a web page Don’t believe me? Try it! New Web Site: Number of Postbacks: [lblCount] Button cmdPost 5/6/2019 Wendi Jollymore, ACES

More on ViewState cmdPost Event Handler: if (ViewState["numPosts"] == null) ViewState["numPosts"] = 0; int num = int.Parse(ViewState["numPosts"] .ToString()); ViewState["numPosts"] = ++num; lblCount.Text = num.ToString(); } 5/6/2019 Wendi Jollymore, ACES

Testing for Postback Sometimes you want code to execute ONLY if this is the first time the page loads Or vice versa The Page is an object It has events, methods, properties IsPostback property True if the current load is a postback (return trip) False if this is the first load 5/6/2019 Wendi Jollymore, ACES

Testing for Postback private void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // first load lblDisplay.Text = "Hello!"); } else { // return trip lblDisplay.Text = "Welcome back!!!"); } 5/6/2019 Wendi Jollymore, ACES

Homework meh 5/6/2019 Wendi Jollymore, ACES