IS 460 MULTI-PAGE PROCESSING LAB

Ekedahl

In this lab, you will work with the following:

·  Cross page postbacks

·  Validation controls

·  Controls including list boxes

Exercise 1: Validation

Start Visual Studio and create a new Web site. Again, I suggest that you do not create the site on your desktop. Make sure to create it on a USB stick.

  1. On the default page (default.aspx), create a prompt and text box in which the user enters their e-mail address.
  2. Use the Validation controls to make sure that the e-mail address is valid. Hint: You can use the RegularExpressionValidator to do this.
  3. Activate the Properties window for this control. Set the ControlToValidate property to the name of the text box you created in the previous steps.
  4. Instead of writing the regular expressions by hand, you can use built-in ones provided by .net. Use the Build button in the ValidationExpression property to activate the following dialog box. Set the property to Internet e-mail address as shown.
  5. Enable client-side validation.
  6. Create a button on the form to submit the page request.
  7. Run the application and click the button without entering an e-mail address. Note that the page is posted even though you did NOT enter an e-mail address. This is because an empty field is valid.
  8. Create a RequiredFieldValidator control and configure it to require that a value be entered into the e-mail text box. Run the program and note the difference in the application’s behavior.
  9. Bellow the label and text box that you just created, create another label and text box in which the user enters their age. The value should be between 18 and 100. You will need both a RequiredFieldValidator and a RangeValidator. Set the ControlToValidate property as necessary for both validators. Next, Set the Type property to Integer for the RangeValidator. Set the Minimum value to 18 and the MaxiumumValue to 100. Test the application to make sure that the validation works correctly.
  10. Next, create a label and prompt to get the birth date from the user. Validate that the entered value is a valid date. The field should be required. The maximum value for the date should be today’s date.
  11. Try on your own to create a compare validator. Create two text boxes to get a password. Use the CompareValidator to make sure that the values of both text boxes are the same.

Exercise 2: Cross-Page Postback

In this exercise, you will add an additional form to the application and configure it so that the form can only be accessed through a cross-page postback.

  1. Create a second ASP page (Web Form) and name it MainForm.aspx.
  2. Create a third form named Error.htm (Make sure to create a simple HTML page, rather than a Web Form. Create some text in the body as follows:
    body
    pError. You cannot get there from here.</p
    </body

Next, you will configure the form named Default.aspx to do a cross page postback to the form named MainForm.aspx.

  1. On the form named Default.aspx, create a button control instance.
  2. Using the Properties window, set the PostBackUrl property to ~/MainForm.aspx. This will cause the button to post to the other form when clicked.
  3. In the Solution Explorer, select the form named Default.aspx. Run the application. On the default.aspx form, enter the required information, and then click the button that will perform the cross-page postback. The form named MainForm.aspx will appear. Note that there is nothing on the form at this point.

Next, you will write the code on the form named MainForm.aspx to test that the form was loaded as a result of a cross page postback. If it is not, an error will be generated.

  1. Create the following code in the Page_Load event handler. This code will transfer control to the page named Error.htm.

protected void Page_Load(object sender, EventArgs e)

{

if (Page.PreviousPage == null)

{

Server.Transfer("Error.htm");

}

else

{

if (!PreviousPage.IsCrossPagePostBack)

{

Server.Transfer("Error.htm");

}

else

{

}

}

}

  1. Now, modify the preceding code to make sure that the previous page is Default.aspx and the form is named form1. That is, you are checking which page caused the cross page postback. Hint: Use PreviousPage.Form.Name (or something like it). If the page that caused the cross page postback is not the default page, then transfer control to the Error.htm page.

Next, you will write the code that will display, on the page named MainForm.aspx, the e-mail address entered in the text box on the previous form.

  1. Enter the following text at the end of the Page_Load event handler.

TextBox txt;

txt = (TextBox)PreviousPage.FindControl("txtEmail");

Response.Write(txt.Text);

  1. Test the application. Try to run the page named MainForm.aspx directly. It will not work and control will transfer to the form named Error.htm.
  2. Run the application again starting from the page named default.aspx. Enter an e-mail address, and then click the button. The second page should appear and the e-mail address should appear from the control on the first page.

ASP applications can be created so that they run a specific start page.

  1. In the Solution Explorer, right click on the project you created. Make sure to right click on the project, rather than the solution.
  2. Select Property Pages from the context menu. Click Start Options. The following dialog box will appear:

  3. Select the Specific page radio button. Click the Ellipses. From the Select Page to Start dialog box, select the form named Default.aspx.
  4. Click OK to close both of the dialog boxes. Now, when the application is run, the default.aspx page will run.

Now, it’s your turn to try some of this on your own.

  1. On the page named MainForm.aspx, create text boxes to store your first name and last name. Create a list box to display the items (Freshman, Sophomore, Junior, and Senior). Create validation controls to make sure that the user entered data into the fields.
  2. Create a button that will perform a cross page postback to the form named Results.aspx. (You will need to create this form.
  3. Now, write the code on the page named Results.aspx that will check that the page is posted from the page named MainForm.aspx. If it was not, display the same error page that you have been displaying.
  4. If the page was posted back from MainForm.aspx, use Response.Write to display the contents of the list box and text boxes from the previous form.

4 | Page