Assignment 2 -- Events, Variables, Methods
1. PageEvents.aspx -- The page illustrates page and control
events. A label control is used to display the output.
-
Create a new web form containing three controls: a textbox, a button and a label.
-
Add an onTextChanged event handler to the textbox and an onClick event handler the
button. Double clicking on the button in VS will automatically create the handler
methods.
-
In the handler method concatenate appropriate text to the label control to show
that the event has fired.
-
Add a Page_Load method and add text to the label each time it fires.
-
Inside the Page_Load method add an if else block that indicates if the page
is posting back or loading.
-
In the Page directive (very top of page) add Trace="true" This will provide the
trace report.
2. Calculator.aspx
-- The calculator
uses the OnClick event associated with each button to call the appropriate method
for each type of calculation. There are five buttons and five methods. Within
each method the appropriate calculation is performed and the result is assigned
to a label control. The five buttons:
-
each has a unique id (such as btnAdd)
-
use the button's text property to define the text that appears on the button
-
use the OnClick property to call a specific method (with a name like "Add_click").
Use Visual Studio's design mode to drag two textboxes and five buttons onto the
design area. Create the button handlers (methods) by double clicking on each
button.
Each method has only a single line of code. The text from the textboxes must
be converted to a double floating point value (e.g.
Convert.ToDouble(tbxInput1.text) to perform the appropriate mathematical
operation (otherwise the two strings are concatenated). The result must then be
converted back to a string and assigned to the label.
C# does not support syntax for exponents. Use the Pow method of the Math class (for
documentation Google ".net math.pow").
3. FontPicker.aspx - This assignment uses a for loop
to iterate through each character of a message entered in a textbox. Each character
is passed to a method named GetImageTag to build a string composed of html image
tags. Look at the source code in the example to see the output. Steps:
-
Start with the class example
FontPickerCSharp.aspx (source).
-
The method GetImageTag(..) is passed a single character. Add a C# switch statement
(or you could use if else if) to select the correct font and write the image
tag. The font name is retrieved from the droplist's SelectedItem.Text property.
-
Concatenate the character directly into the file name. For instance:
case "Chunk Red":
return strPath + "Chunk/red/" + strChar + "9.jpg'>";
case ...
-
The local variable strPath contains the common portion of the image urls. The common
part of the path for all images is:
"<img src='/sandvig/images/alphabet/"
-
Each font/color combination is stored in a separate folder and uses a slightly different
naming convention. The directory / naming convention for each font is (where x is
the character desired).
|
Font
|
Directory & File
|
|
Chunk Red
|
chunk/red/x9.jpg
|
|
Deco Blue
|
deco/blue/x1.gif
|
|
Animals
|
animals/x4.gif
|
|
Elegant Red
|
elegant/red/4x.gif
|
|
Funky
|
funky/x3.jpg
|
|
Tape Punch
|
punch/black/x7.gif
|
-
Look at the html source code on the working sample to see the image tags sent to
the browser. (optional: to add line breaks to your html source code include the
line break escape character (\n) in your output string. This won't affect the appearance
of the rendered html, only that of the html source code.)
-
Replace spaces in the message with <br /> tags. This is easily done in StringBuilder
by making the first test in the If then statement:
if (strChar == " ") ...
-
Most ASP.NET server controls will write client-side JavaScript that will automatically
post the form when the value in the control is changed by the user. To have your
form automatically posted when the user selects a different font add the following
to your dropdown list control:
AutoPostBack="true" OnSelectedIndexChanged="btnSubmit_Click"
where "btnSubmit_Click" is the name of the handler for the button click event.
-
By default the contents of all server controls are automatically stored in the viewstate.
While this feature is often convenient the viewstate field can get quite large if
the controls hold a lot of data. In FontBuilder.aspx the label control that contains
the html image tags can be quite large. We don't need this data to be stored in
the page viewstate so turn the viewstate off for the label control by adding to
its tag: EnableViewState = "false". Compare the difference between the size of the
hidden viewstate in the example above to FontBuilderViewState.aspx which has viewstate on (for dramatic
results put in a long message).
4. AnalogClock.html
-- The .NET class library includes several classes for image generation and manipulation.
Just for fun, let's cut and paste the code for an analog clock. The clock isn't
very useful but it is a good illustration what can be done with .NET. Copy and paste
the following two files. AnalogClock.html (view source from sample) is a simple
html file with an image tag and a metarefresh tag. It calls "ClockImage.aspx"
(view source)
which generates a clock image with the current time.
To submit your assignment for grading send an email with the URLs for your assignment
to:
- Professor Sandvig and the TA at
(note: this address is for homework assignments only. Please send correspondence
to
)
- 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.