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.
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>