Answers for Day 5

IOTA^_^    

Sams Teach Yourself ASP.NET in 21 Days, Second Edition
By Chris Payne
Table of Contents
Appendix A.  Answers to Quiz Questions


Quiz

1:

How do Web forms keep track of viewstate?

A1:

Through hidden form variables.

2:

What does the state bag do, and how do you access it?

A1:

The state bag allows you to store custom values (values that aren't entered in controls) between form posts. You can access it through the ViewState variable:

 ViewState("MyItem") = MyValue 
3:

True or false: Events in Web forms are processed before the Page_Load event.

A1:

False. The Page_Load event is always processed first, followed by the events, in no particular order.

4:

What is the standard event parameter list?

A1:

 (Sender as Object, e as EventArgs) 
5:

What is the importance of the runat="server" tag?

A1:

This tag converts an HTML element to a server control. Without it, the server couldn't keep track of controls, the viewstate wouldn't be maintained, and you couldn't have programmatic access to the control on the server. This is a key element of Web forms.

6:

Why should you use HTML server controls?

A1:

To convert existing HTML elements into server controls that you can manipulate through code.

7:

What's wrong with the following code snippet?

 <asp:Button     Click="HandleThis" > 
A1:

It's missing the runat="server" attribute. The event declaration should read OnClick="HandleThis". And there's no closing tag: </asp:Button>.

8:

Which property can be used to make a control post immediately to the server upon an event?

A1:

 AutoPostBack 
9:

When should you use Web controls instead of HTML server controls?

A1:

Anytime you create new Web forms (that is, when you're not converting old pages).

Exercises

1:

Build an application for choosing a baby's name. The user will select the sex of the baby, and then a ListBox will display a few possible names. When the user selects a name, a message will be displayed using the selected name.

A1:

The code is as follows:

 1:    <%@ Page Language="VB" %> 2: 3:    <script runat="server"> 4:       sub ChangeNames(Sender as Object, e as EventArgs) 5:          if rlSex.SelectedIndex = 0 then 6:             'female 7:             lbNames.Items(0).Text = "Sally" 8:             lbNames.Items(1).Text = "Sara" 9:             lbNames.Items(2).Text = "Hope" 10:             lbNames.Items(3).Text = "Kaylee" 11:          else 12:             'male 13:             lbNames.Items(0).Text = "John" 14:             lbNames.Items(1).Text = "Chris" 15:             lbNames.Items(2).Text = "Daniel" 16:             lbNames.Items(3).Text = "Walter" 17:          end if 18:       end sub 19: 20:       sub DisplayMessage(Sender as Object, e as EventArgs) 21:          lblMessage.Text = Sender.SelectedItem.Text & _ 22:             " is a wonderful name!" 23:       end sub 24: 25:    </script> 26: 27:    <html><body> 28:       <form runat="server"> 29:          <asp:Label  runat="server"/><p> 30: 31:          Choose the sex of your baby: 32:          <asp:RadioButtonList  runat="server" 33:             OnSelectedIndexChanged="ChangeNames" 34:             AutoPostBack="true" > 35:             <asp:ListItem>Female</asp:ListItem> 36:             <asp:ListItem>Male</asp:ListItem> 37:          </asp:RadioButtonList><p> 38: 39:          Possible names:<br> 40:          <asp:ListBox  runat="server" 41:             OnSelectedIndexChanged="DisplayMessage" 42:             AutoPostBack="true" > 43:             <asp:ListItem></asp:ListItem> 44:             <asp:ListItem></asp:ListItem> 45:             <asp:ListItem></asp:ListItem> 46:             <asp:ListItem></asp:ListItem> 47:          </asp:ListBox><p> 48: 49:       </form> 50:    </body></html> 
2:

Build a standard user registration page with the following fields: name (required), address, phone (required), fax, and e-mail address (required). If any of the required values are missing, display an error message to the user.

A1:

The code is as follows:

 1:    <%@ Page Language="VB" %> 2: 3:    <script runat="server"> 4:       sub Submit(Sender as Object, e as EventArgs) 5:          if tbName.Text = "" then 6:             lblMessage.Text = "You forgot your name!<br>" 7:          end if 8:          if tbPhone.Text = "" then 9:             lblMessage.Text += "You forgot your phone!<br>" 10:          end if 11:          if tbEmail.Text = "" then 12:             lblMessage.Text += "You forgot your email!<br>" 13:          end if 14: 15:          if lblMessage.Text = "" then 16:             lblMessage.Text = "Your information has been " & _ 17:                "submitted!" 18:          end if 19: 20: 21:       end sub 22:    </script> 23: 24:    <html><body> 25:       Please enter the following information.<p> 26: 27:       <asp:Label  runat="server"/> 28:       <form runat="server"> 29:          <table cellpadding="2" cellspacing="2" border="1"> 30:          <tr> 31:             <td width="250">Name* </td> 32:             <td width="150"> 33:                <asp:TextBox  runat="server"/> 34:             </td> 35:          </tr> 36:          <tr> 37:             <td>Address</td> 38:             <td> 39:                <asp:TextBox  runat="server"/> 40:             </td> 41:          </tr> 42:          <tr> 43:             <td>Phone* (area code first)</td> 44:             <td> 45:                <asp:TextBox  runat="server"/> 46:             </td> 47:          </tr> 48:          <tr> 49:             <td>Fax (area code first)</td> 50:             <td> 51:                <asp:TextBox  runat="server"/> 52:             </td> 53:          </tr> 54:          <tr> 55:             <td>Email*</td> 56:             <td> 57:                <asp:TextBox  runat="server"/> 58:             </td> 59:          </tr> 60:          <tr> 61:             <td colspan="2"> 62:                <asp:Button  runat="server" 63:                   Text="Submit" 64:                   OnClick="Submit" /> 65:             </td> 66:          </tr> 67:          </table> 68:          <p> 69:          <i>An asterisk (*) indicates a required field.</i> 70:       </form> 71:    </body></html> 


    IOTA^_^    
    Top


    Sams Teach Yourself ASP. NET in 21 Days
    Sams Teach Yourself ASP.NET in 21 Days (2nd Edition)
    ISBN: 0672324458
    EAN: 2147483647
    Year: 2003
    Pages: 307
    Authors: Chris Payne

    Similar book on Amazon

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