Chapter 6
This chapter discusses how ASP.NET revolves around an event-driven model, how things occur in strict order, and ways in which the ASP.NET page can react to user intervention. The solution files for some of the exercises are too long to be included here – these have been put up for download on the Wrox Web site.
Exercise 1
Explain why event-driven programming is such a good way of programming for the Web.
Solution
Event-driven programming is a good way of programming for the Web because it mirrors user interaction with Web pages.
Exercise 2
Modify an HTML form to add a set of ASP.NET Server Controls so that the information in the form is retained when the submit button is pressed.
Solution
The following code solves this problem (available in the code download as 57084_Ch06_Ans2.aspx ):
<%@ Page Language="C#" %>
<html>
<head>
<title>Chapter 6 Question 2 Solution</title>
</head>
<body>
<form runat="server">
<p>
Please enter your name:
<asp:TextBox id="txtName" runat="server"></asp:TextBox>
</p>
<p>
What would you like for breakfast?
<asp:CheckBoxList id="cblWhatToEat" runat="server">
<asp:ListItem Value="Cereal">Cereal</asp:ListItem>
<asp:ListItem Value="Eggs">Eggs</asp:ListItem>
<asp:ListItem Value="Pancakes">Pancakes</asp:ListItem>
</asp:CheckBoxList>
</p>
<p>
When would you like breakfast?
<asp:RadioButtonList id="rblWhenToEat" runat="server">
<asp:ListItem Value="Now">Now</asp:ListItem>
<asp:ListItem Value="Later">Later</asp:ListItem>
</asp:RadioButtonList>
</p>
<p>
<asp:Button id="Button1" runat="server"
Text="Place Your Order"></asp:Button>
</p>
</form>
</body>
</html>
Exercise 3
Add a Page_Load() event handler to the ASPX code you've just created for Exercise 2, to confirm the selections made in the following format:Thank you very much ______.You have chosen ____ for breakfast. I will prepare it for you ______.
Solution
The required changes are highlighted below.
<%@ Page Language="C#" %>
<script runat="server">
void Page_Load()
{
if (IsPostBack)
{
lblConfirmation.Text = "Thank you very much "
+ txtName.Text + ".<br />";
lblConfirmation.Text += "You have chosen " +
cblWhatToEat.SelectedValue + " for breakfast. ";
lblConfirmation.Text += "I will prepare it for you " +
rblWhenToEat.SelectedValue + ".";
}
}
</script>
<html>
<head>
<title>Chapter 6 Question 3 Solution</title>
</head>
<body>
<form runat="server">
...
...
<asp:Button id="Button1" runat="server"
Text="Place Your Order"></asp:Button>
</p>
<p>
<asp:Label id="lblConfirmation" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
Exercise 4
Create a very basic virtual telephone using an ASPX file that displays a textbox and a button named Call. Configure your ASPX file so that when you type a telephone number into the textbox and press Call, you are:
Presented with a message confirming the number you are calling
Presented with another button called Disconnect, which when pressed, returns you to your opening page, leaving you ready to type another number
Solution
The following code will generate these results for you (also available as 57084_Ch06_Ans4.aspx ):
<%@ Page Language="C#" %>
<script runat="server">
void DialNumber(object sender, EventArgs e) {
lblConfirmation.Text = "Dialling " + txtPhoneNumber.Text;
btnDisconnect.Visible = true;
}
void DisconnectNumber(object sender, EventArgs e) {
lblConfirmation.Text = ";
btnDisconnect.Visible = false;
}
</script>
<html>
<head>
<title>Chapter 6 Question 4 Solution</title>
</head>
<body>
<form runat="server">
<p>
Number to Dial:
<asp:TextBox id="txtPhoneNumber" runat="server"></asp:TextBox>
<asp:Button id="btnCall" onclick="DialNumber"
runat="server" Text="Call"></asp:Button>
</p>
<p>
<asp:Label id="lblConfirmation" runat="server"></asp:Label>
</p>
<p>
<asp:Button id="btnDisconnect" onclick="DisconnectNumber"
runat="server" Text="Disconnect"
Visible="False"></asp:Button>
</p>
</form>
</body>
</html>