Answers for Day 4

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:

What are static members?

A1:

Static members are members of an object that don't belong to any specific instance of the object, such as a counter. These can be accessed by the following:

 Object.member 
2:

Will the following code snippet work?

 <html><body>    Hi there!    <%       Response.Redirect("page2.aspx")    %> </body></html> 
A1:

No. Response.Redirect must be called before any output is sent to the browser.

3:

What language is the following code snippet written in?

 Response.Write("Hello there!") 
A1:

 Response.Write("Hello there!") 

This is VB.NET. Often the only giveaway between the languages is the semicolon at the end of the statement.

Exercises

1:

Create an ASP.NET page in C# that acts as a secure login page. Allow the user to enter a username and password in a text box. If they match a string you specify, redirect the user to a "success" page. Otherwise, display an error. If the user is valid, save her username in a Session variable.

A1:

The code for example1.aspx is as follows:

 1:    <%@ Page Language="C#" %> 2: 3:    <script runat="server"> 4:       void Submit_Click(Object Sender, EventArgs e) { 5:          if (tbPass.Value == "mypass") { 6:             Session["UserName"] = tbUser.Value; 7:             Response.Redirect("exercise2.aspx"); 8:          } else { 9:             Label1.Text = "<font color=red>That " + 10:                "is the wrong password!</font>"; 11:          } 12:       } 13:    </script> 14: 15:    <html><body> 16:       <form runat="server"> 17: 18:          Please enter your username and password:<p> 19:          <input type="text"  20:             runat="server"/><br> 21:          <input type="password"  22:             runat="server"/> 23: 24:          <p> 25:          <asp:Button  26:             text="Submit" 27:             runat="server" 28:             OnClick="Submit_Click" /><p> 29: 30:          <asp:Label  runat="server"/> 31:       </form> 32:    </body></html> 
2:

Write the success page in VB.NET. Display a personalized Welcome message using the Load event of the Page object and a Label control. Also inform the user of the current time and his Session ID. Check the IsPostback property of the Page object to ensure that this message is only displayed the first time he arrives at the page. Provide a button for him to log out, with a confirmation message.

A1:

The source for exercise2.aspx is as follows:

 1:    <%@ Page Language="VB" %> 2: 3:    <script runat="server"> 4:       sub Page_Load(Sender as Object, e as EventArgs) 5:          if not Page.IsPostBack then 6:             Label1.Text = "Welcome " & Session("Username") & _ 7:                "!<p>" 8: 9:             Label1.Text = Label1.Text & "The current time " & _ 10:                "is: " & DateTime.Now.ToString("T") & "<p>" 11: 12:             Label1.Text = Label1.Text & "Your session id " & _ 13:                "is: " & Session.SessionID & "<p>" 14:          end if 15:       end sub 16: 17:       sub Submit_Click(Sender as Object, e as EventArgs) 18:          Session.Abandon 19:          Label1.Text = "Your session has ended." 20:          btSubmit.Visible = false 21:       end sub 22:    </script> 23: 24:    <html><body> 25:       <form runat="server"> 26:          <asp:Label  runat="server"/> 27: 28:          <asp:Button  29:             text="Submit" 30:             runat="server" 31:             OnClick="Submit_Click" /><p> 32:       </form> 33:    </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