Assignment 4 -- User Controls and Maintaining State
-- User controls are used to eliminate duplicate user interface (UI) code.
Any UI code that is used in more than once (headers, footers,
search boxes, menus, etc.) is a good candidate for a user control. User controls share many
attributes with .aspx
pages, including page events (page_load, etc.) and event handlers. They may contain
other web controls, public properties for getting and setting values, and support caching.
This exercise uses two web forms (default.aspx & SearchBrowse.aspx) and three
user controls(Header.ascx, Footer.ascx
and LeftMenu.ascx). The header, footer and LeftMenu user controls are used in both
of the .aspx pages. This exercise is the start of the music store project.
Your pages should have a distinct header, footer and menu. Background colors or
borders are a good way to make them distinct.
All modern web sites
should be mobile friendly. CSS Frameworks
most convenient way to build mobile friendly sites and also provide many other
useful features. Both this exercise and sample music store (XML Music) utilize the
Twitter Bootstrap framework.
You may use either Bootstrap or a different CSS framework,
but you must use a framework for both this exercise and
the music store project.
- Since this assignment uses multiple files you should create a new folder named
within your A04 folder.
- You may copy the HTML & CSS from the sample.
- The class handout "usercontrols" may be a userful reference.
it, and add it to your UserControls folder. Bootstrap includes
a large number of .css and .js files. Initially you will use only one .css file but
should copy all the files in case you later want to use additional bootstrap
features. The zip file contains the full framework.
In a real-world web site you would visit Bootstrap's web site
and download customized files that
include only the features used by your site.
- The Bootstrap documentation
explains how the grid system works. Read the first two
sections "Overview" and "Grid System" to learn the basics.
- Create a new page named default.aspx. Copy and paste in the header tags from the working sample
to link the
stylesheets. Also include the viewport tag, which is required by Bootstrap.
- In Visual Studio add new item "Web User Control." In the dialogue box
unclick "place code
in separate file" and name the new file header.ascx.
- Add appropriate HTML to the user control.
- Register the user control in your page
by switching Visual Studio design view and dragging the .ascx file onto the design surface
and dropping it where you want the control to appear on the page.
- Repeat the previous steps for your leftMenu and footer controls.
- leftMenu.ascx requires anchor tags, a textbox and a button as shown
in the sample. Name the textbox "tbSearch".
Create a handler for the
button. The button handler is located in the ascx file and contains a single line of code.
Response.Redirect("SearchBrowse.aspx?Search=" + tbSearch.Text);
- SearchBrowse.aspx - This page retrieves the user's search/browse query from the
querystring and displays it in a label. There are two ways a user can
get to this page: clicking on a browse style anchor tag or typing
a search query. You need to know which method they used so that you
can handle it correctly. The anchor tags use a parameter
named "style" and the search uses a parameter named "query".
SearchBrowse.aspx looks for these parameters in the querystring.
- Create SearchBrowse.aspx by copying default.aspx.
- In the page_load hander use Request.QueryString["style"] to retrieve the
style name from the querystring and assign it to a variable name
"style". Do the same for "query."
- Check which parameter was found with:
if (style != null)...
and assign the value to the label.
2. CarSelect1.aspx -- Cookies are frequently used for
maintaining state. A major advantage of cookies is that they persist between user
sessions. Here is a good overview of using ASP.NET cookies.
- CarSelect1.aspx: Create a new web form and add a textbox and a
button. Create a handler for the button by double-clicking on the button in design
view. The handler writes the user's name to a cookie and redirects to the next step.
The syntax for writing a cookie named CarSelectName is:
Response.Cookies["CarSelectName"].Value = tbName.Text;
Note that since cookies are collections the square brackets are used to refer to
- Copy CarSelect1.aspx and replace the textbox and button with a RadioButtonList
- Add three ListItems to the RadioButtonList, one for each car model.
- Set the AutoPostBack property to true.
- Create a handler for the postback by switching to design view and double clicking
on the control.
- Write the information to a new cookie named CarSelectModel.
- Add an anchor tag at the bottom of the page that points to CarSelect1.aspx.
- In the Page declaration (first line in file) add Trace = "true". This provides
a report at the bottom of the page that displays the content of your cookie. A very
useful feature for debugging.
- CarSelect3.aspx - This page is very similar to CarSelect2 except
it uses a DropDownList instead of a RadioButtonList.
- CarSelect4.aspx - Displays the content of the cookies and an appropriate
- Add labels for Name, Color and Model. Add a Image control to display the image.
- Create a Page_load method that retrieves the values from the cookies and assigns
them to the appropriate labels.
- The syntax for the ImageURL is something like:
imgCar.ImageUrl = "http://yorktown.cbe.wwu.edu/sandvig/mis324/assignments/A04/carImages/"
+ lblModel.Text + "-" + lblColor.Text + ".jpg";
You may use the images from my folder. There is no benefit in copying the images
into your directory.
-- Session objects are easy to use and quite handy for many applications, especially
those that require a login. Session objects cannot be seen or altered by the client
and timeout after a period of inactivity.
This assignment consists of two pages: Session.aspx and Login.aspx. Session.aspx
is the page containing content that we want to protect with a login. It checks to
make sure the user has a session object, and if not, redirects the user to Login.aspx.
The login page asks for a name and password, checks the user's password, and if
correct, creates a session object containing the user's name then redirect the user
back to Sesson.aspx.
- The class handout "MaintainingState" may be a useful reference.
- Login.aspx uses a single event handler which is triggered when the user clicks the
login button. The handler contains a single if statement which checks the password.
If the password is correct it creates a session object named Session["UserName"]
which contains the user's name and the user is redirected to Session.aspx. If the password is
incorrect then a label is used to ask the user to try again.
- Secure.aspx uses two subroutines. The first, Page_load, checks to see if the session
object exists. The syntax to check for the existence of the Session object is:
if (Session["UserName"] == null) ....
If there is no session object then the user is redirected to Login.aspx. If the
session does exist then we assign the username to a label and display it to the
- The second handler in Session.aspx is triggered by the "Logout" button. This
handler uses the Session.Abandon() method to kill the session then redirects the
user back to the login page.
- Here are links to documentation for the session object and Response.Redirect
In this exercise you will add validation for name and breed to the class example dog.cs. You will
also create a web form that adds and retrieves data to the class. Steps:
- Use Visual Studio to create a new class named dog. Visual Studio will prompt you
to create a new folder named App_Code. Click yes.
- Replace the code in dog.cs with the code from the class example
- Create a new web form with four text boxes, a button and a label similar to the sample.
Provide default values in the textboxes (for ease of testing).
- Add a button handler that initializes a dog object and
assigns the user inputs to its properties. (similar to the class
- Assign the text from the method DescribeThisDog to a label using syntax similar to:
lblMessage.Text += myDog.DescribeThisDog();
- Use a Try Catch block when assigning properties to the dog object. Error messages should
be displayed in the label, as in the working sample.
- Test your code
- Add validation to dog.cs for name and breed. Both properties should have a length > 0.
An appropriate error message should be thrown if data is not valid.
To submit your assignment for grading send an email with the URLs for your assignment
- Professor Sandvig and the TA at
(note: this address is for homework assignments only. Please send correspondence
- cc. a copy to yourself
The subject line of your email should read "MIS324 AXX YourName"
where XX is the assignment number. Please check that your URLs are correct before
submitting them for grading. Files with incorrect URLs will not be graded.
When pages are connected via navigation (as in your Music store project) it is only
necessary to submit the URL of the first page.