Download presentation
Presentation is loading. Please wait.
Published byBrooks Holler Modified over 9 years ago
1
1 CS 3870/CS 5870: Lab4 Save with Invalid Price Keep Order After Updating
2
2 Lab 5 Web User Controls Copy Folder Lab4 or Create New Folder Lab5?
3
3 Creating Web User Controls Add New Items File name *.ascx Add controls on the user controls Use Position Relative (default) Set control properties Add HTML tags, e.g.
4
4 ShoppingItem.ascx.vb Partial Class ShoppingItem Inherits System.Web.UI.UserControl ‘ Make it easy to use ‘ (or make set/get methods) Public theID, theName As String Public thePrice, theCost As Double Public theQuantity As Int32 End Class
5
5 Page (Control) Load Event Partial Class ShoppingItem Inherits System.Web.UI.UserControl Protected Sub Page_Load(...) Handles Me.Load txtID.Text = theID txtName.Text = theName txtPrice.Text = FormatCurrency(thePrice) txtQuantity.Text = quantity txtCost.Text = FormatCurrency(theCost) End Sub End Class
6
Page Checkout Without Master Page (Default2.aspx) 6
7
7 Loading User Controls at Design Time Build Web Site Design view of a web page (Default2aspx) Drag the ShoppingItem.ascx from the solution explorer to the page (Checkout.aspx) In the page load ShoppingItem1.theID = "P101" ShoppingItem1.thePrice = 120.5
8
8 Reference to User Controls ‘ A new line (page directive) was added to Checkout.aspx when dragging it to the page ‘ in Source View
9
9 Loading User Controls at Run Time Must have the reference on the page to use the control (Dragging or Copy/Paste)
10
10 Page Shopping.aspx You can still use DataTable as in Lab4 Dim c1 As ShoppingItem Dim bag As ArrayList Protected Sub Button1_Click(...) Handles btnAdd.Click If Session("myBag") Is Nothing Then bag = New ArrayList Else bag = Session("myBag") End If c1 = CType(LoadControl("ShoppingItem.ascx"), ShoppingItem.ascx) c1.theID = TextBox1.Text c1.theName = TextBox2.Text c1.price = TextBox3.Text bag.Add(c1) Session("myBag") = bag End Sub
11
11 Page Default2.aspx ‘ Declare c1, c2 As ShoppingItem & bag As ArrayList Protected Sub Page_Load(...) Handles Me.Load If Session("myBag") Is Nothing Then Exit Sub End If bag = Session("myBag") Dim myControl As Control = Page.FindControl("Form1") For x As Integer = 0 To bag.Count - 1 c1 = CType(LoadControl("ShoppingItem.ascx"), ShoppingItem) c2 = bag(x) c1.theID = c2.theID c1.theName = c2.theName c1.thePrice = c2.thePrice c1.theQuantity = c2. theQuantity c1. theCost = c2.theCost myControl.Controls.Add(c1) Next End Sub
12
12 Formatting ShoppingItem Same as other web pages Adding
13
13 Page Default2.aspx: Panel Add Panel1 Set position of both textbox1 and Panel1 to Absolute Adjust their positions Inside Default2.aspx.vb Dim myControl As Control = Page.FindControl("form1") myControl = Page.FindControl("Panel1") Dim total As Single = 0.0 For x As Integer = 0 to bag.Count - 1... total += c1._cost myControl.Controls.Add(c1) Next TextBox1.Text = FormatCurrency(total)
14
Checkout: Using Master Page Add Panel1 to the master page Set position to Absolute Adjust position 14
15
15 Checkout.aspx with Master Page ‘ Master: VB function to return the master page Dim myControl As Control = Master.FindControl(“form1”).FindControl(“Panel1”) Dim total As Single = 0.0 For x As Integer = 0 to bag.Count - 1... total += c1._cost myControl.Controls.Add(c1) Next TextBox1.Text = FormatCurrency(total)
16
16 Cannot Cast Move to another PC if you see the following Cannot cast ASP.ShoppingItem to ShoppingItem
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.