Handling Events and Postbacks


Listing 22.1 gave you a brief look at handling server-side events through ASP.NET pages with a submit button. To illustrate how code typically utilizes both the IsPostback property of the page as well as multiple event handlers per form, Listing 22.2 shows you the ASPX code for a page that presents the user with a simple survey, accepts two different button clicks, and presents the user with results. Listing 22.3 contains the C# code for the event handlers in the partial class.

Listing 22.2. Handling Multiple Events, ASPX Code

<%@ Page Language="C#" AutoEventWireup="true"     CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>Untitled Page</title> </head> <body>     <form  runat="server">     <div>         <asp:Label  runat="server" Text="Label"></asp:Label><br />         <asp:Panel  runat="server">             Name:&nbsp;<asp:TextBox  runat="server"></asp:TextBox><br />             Favorite Color:&nbsp;             <asp:ListBox  runat="server">                 <asp:ListItem Text="White" Value=1 />                 <asp:ListItem Text="Green" Value=2 />                 <asp:ListItem Text="Yellow" Value=3 />             </asp:ListBox><br />             <asp:Button  runat="server"              Text="Submit Survey" OnClick="btnSubmit_Click" />             <asp:Button  runat="server"              Text="Cancel" OnClick="btnCancel_Click" />&nbsp;         </asp:Panel>         <asp:Panel  runat="server">             <asp:PlaceHolder  runat="server" />         </asp:Panel>     </div>     </form> </body> </html> 

The page in Listing 22.2 has two panels: one panel for input, and one panel for displaying the results. The idea is to hide the input panel during the postback while revealing the results panel. When the page is first displayed, just the input panel is displayed because the results panel has no controls in the placeholder.

Listing 22.3. Handling Multiple Events, C# Code

[View full width]

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {     if (!Page.IsPostBack)     {         lblWelcome.Text = "Greetings. Please take a few seconds to fill out this survey.<br/>";         pnlResults.Visible = false;         pnlInput.Visible = true;     }     else     {         pnlResults.Visible = true;         pnlInput.Visible = false;     } } protected void btnSubmit_Click(object sender, EventArgs e) {        // store survey data in database, etc.        lblWelcome.Text = "Thanks for submitting your survey.";        plhResults.Controls.Add(new LiteralControl(             string.Format("You said your name was <b>{0}</b>, and your favorite color  is <b>{1}</b> ({2})",             txtName.Text, lbColors.SelectedItem.Text,             lbColors.SelectedItem.Value))); } protected void btnCancel_Click(object sender, EventArgs e) {     lblWelcome.Text = "You cancelled your survey submission. Nothing was saved."; } } 

When you run the code in the preceding listings, you will be presented with an input form. You can either fill it out and click Submit, which will show you the information you entered (remember that information is reconstructed from view state during the Initialize stage of the page life cycle), or you can click Cancel and be informed that the results of the survey weren't saved. This provides a good example of where it is appropriate to check for IsPostback and when to perform tasks specific to an individual event.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net