240 likes | 256 Views
Explore how ASP.NET pages are processed, server controls, postback event handling, ViewState usage, and state management examples. Learn about the interaction between the server and the browser, control properties, and hidden ViewState fields. Dive into ASP.NET projects and controls rendering in HTML.
E N D
Session 8.2 • Postback, ViewState • How ASP.NET pages are processed
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” 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 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 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! 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 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! Wendi Jollymore, ACES
Postback and State - Example • Make this: (names in the notes) Wendi Jollymore, ACES
Postback and State - Example • cmdGetName_Click() event: if (!txtName.Text.Equals("")) lblDisplayName.Text = "Hello, " + txtName.Text; 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; } 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 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 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 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! 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 Wendi Jollymore, ACES
Postback and State - Example • How it works: • On second visit to server: • ViewState updated: lblDisplayName Text contains “Hello, Kaluha” • On second visit to server: • Before cmdGetNum event handler • ViewState is examined and all controls updated accordingly • After cmdGetNum event handler • ViewState updated: lblDisplayGuess Text contains “You guessed wrong.” 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”) 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…. 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. 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 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(); } 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 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!!!"); } } Wendi Jollymore, ACES
Homework • meh Wendi Jollymore, ACES