3.3 Practical 3 (Web Programming)
3.3 Practical 3 (Web Programming)
Practical Exercise 3
• Create a new ASP.NET page named [Exercise1.aspx] with the following layout:
DropDownList: ddlFruit
Literal: litPrice
DropDownList: ddlQuantity
Button: Button:
btnSubmit btnReset
Literal: litResult
• Program the Page_Load event handler so that 3 fruits (Apple, Orange and Banana) are to be
added toddlFruit at runtime:
A string array
of fruit names
• Hit [Ctrl + F5] to run the ASP.NET without debugging (faster). Do you get a list of fruits? Click
on the [Submit] button several times. What is happened to the list of fruits now?
NOTE: The Page_Load event handler runs every time the page is being loaded.
• Now, surround the original codes with the following if statement block. What is the result?
• Within the same if statement block, add the following codes to dynamically populate
ddlQuantity with number from 1 to 10 during runtime:
• Create a function named getPrice() which accepts a fruit name (string) as input parameter,
and returns the unit price (decimal) based on the following table:
Fruit Price
Apple 1.50
Orange 1.00
Banana 0.50
• In Design View, select ddlFruit. Tick the [Enable AutoPostBack] option from its smart tag (or
set its AutoPostBack property to True from the Property Window).
NOTE: Setting AutoPostBack property to True will cause a DropDownList to trigger a page
postback action (i.e. immediate page submission) as its selected value is being changed.
• Hit [Ctrl + F5] to test the page. Try to change the selected fruit. Do you find any problem?
• To overcome the problem, manually call the event handler as during the initial page load.
Add the following codes to the Page_Load event handler:
• Program the btnSubmit_Click event handler. When btnSubmit is clicked, the total price will
be calculated and displayed on litResult (as shown in the following screenshot):
• Program the btnReset_Click event handler. When btnReset is clicked, clear all the input and
output fields. A shortcut to achieve so is to reload the same page as shown below:
NOTE: The shortcut may not be suitable for all circumstances. In certain cases, resetting the
input and output fields individually may be the only option.
• Lastly, add the following of JavaScript code to the OnClientClick property of btnReset. This
will cause a confirmation box to be shown (i.e. client-side event) before the [Reset] triggers
the postback action.
• Create a new ASP.NET page named [Exercise2.aspx] with the following layout:
Label: lblResult
Buttons: namely
btn1, btn2, …, btn0
• Add a new CSS file to the project with the following CSS rules. Then, link the CSS file to the
page (i.e. drag-and-drop the CSS file to the page in Design View):
• In Design View, select ALL the buttons. Achieve so by pressing down the [Ctrl] key while
selecting each of the buttons using mouse click. Set their CssClass property to [button].
• Hit [Ctrl + F5] to run the ASP.NET page without debugging. The resulting page is as follows:
• When any of the numbered buttons is clicked, append the respective digit to lblResult (with
a maximum of 10 digits). Since all of the numbered buttons (btn1, btn2, …,btn0) are having
the same programming logic, it is easier to attach all of them to a single event handler.
• Start by setting the CommandArgument propertyof each of the numbered buttons to its
respective digit. For example:
Control Property
btn1 • CommandArgument = 1
btn2 • CommandArgument = 2
… • CommandArgument = …
btn0 • CommandArgument = 0
• In Design View, select all of the numbered buttons (btn1, btn2, …,btn0). Then click on the
Event tab (i.e. the lighting icon) in the Property Window. Type [buttons_Command] for the
Command event and press [Enter].
NOTE: The main reason for choosing the Command event (rather than Click event) is the
Command event allows us to easily obtain the CommandArgument as attached to each of
the numbered buttons.
Only a maximum of 10
digits can be displayed
• In Design View, double-click on btnC(cancel) to create the btnC_Click event handler. When
btnC is clicked, remove the last digit from lblResult.
• In Design View, double-click on btnR (reset) to create the btnR_Click event handler. When
btnR is clicked, reset the page to its initial state.