CS 3870/CS 5870 Web User Controls
Prog 7 Folder Prog7 Sub-folders Admin Sub-folders Member Prog7MasterPage Default.aspx ShoppingItem.ascx Sub-folders Admin Updating.aspx Sub-folders Member Shopping.aspx Chckout.aspx
Creating ShoppingItem.ascx For page Checkout Add New Items Web User Control File name ShoppingItem.ascx Place code in separate file Different file extension
Adding Controls to ShoppingItem.ascx Same as adding server controls to Web Forms Using default (relative) position, not absolute position Textboxes and label on same line
Setting Properties of Controls Read Only Width Alignment Using Style
Class Prog7_ShoppingItem Partial Class Prog7_ShoppingItem Inherits System.Web.UI.UserControl ‘ Private data Private _theID, _theName As String Private _thePrice, _theCost As Single Private _theQuantity As Integer End Class
Public Properties Partial Class Prog7_ShoppingItem Inherits System.Web.UI.UserControl Public Property theID As String Set(value As String) _theID = value End Set Get Return _theID End Get End Property . . . End Class
Public Properties Partial Class Prog7_ShoppingItem Inherits System.Web.UI.UserControl Public Property theQuantity As Integer Set(value As String) _theQuantity = value End Set Get Return _theQuantity End Get End Property . . . End Class
Public Properties Partial Class Prog7_ShoppingItem Inherits System.Web.UI.UserControl Public Property theCost As Single Set(value As Single) _theCost = value End Set Get Return _theCost End Get End Property . . . End Class
Page Load Event Partial Class Prog7_ShoppingItem Inherits System.Web.UI.UserControl . . . Protected Sub Page_Load(. . .) Handles Me.Load lblMsg.Text = “” txtID.Text = _theID txtName.Text = _theName txtPrice.Text = FormatCurrency(_thePrice) txtQuantity.Text = _theQuantity txtCost.Text = FormatCurrency(_theCost) End Sub End Class
Adding Web User Control at Design Time Prog7/Default.aspx Source view (or design view) Drag and Drop ShoppingItem.ascx to Prog7/Default.aspx If error, close and re-open Default.aspx or re-open VS
Page Directive Register Prog7/Default.aspx Source view <%@ Register Src="~/Prog7/ShoppingItem.ascx" TagPrefix="uc1" TagName="ShoppingItem" %> <asp:Content ID="Content1" . . .Runat="Server"> <h2>Use the TreeView to . . . </h2> <uc1:ShoppingItem runat="server" ID="ShoppingItem" /> </asp:Content>
Page Default.aspx Protected Sub Page_Load(. . .) Handles Me.Load ShoppingItem.theID = "101" ShoppingItem.theQuantity = 20 End Sub
Adding Web User Control at Run Time Must register the control at the design time Drag and Drop or Copy and Paste <%@ Register Src="~/Prog7/ShoppingItem.ascx" TagPrefix="uc1" TagName="ShoppingItem" %>
Shopping Bag Use SortedList
Global.asax Session("Prog7_Bag") = New SortedList
Page Shopping.aspx Protected Sub btnAdd_Click(. . .) Handles btnAdd.Click Dim c1 As Prog7_ShoppingItem Dim bag As SortedList = Session("Prog7_Bag") ‘ Specify file path to load the control ‘ c1 = CType(LoadControl("../ShoppingItem.ascx"), ‘ Prog7_ShoppingItem) ‘ New works here c1 = New Prog7_ShoppingItem End Sub
Page Shopping.aspx Protected Sub btnAdd_Click(. . .) Handles btnAdd.Click . . . c1 = New Prog7_ShoppingItem c1.theID = txtID.Text c1.theQuantity = txtQuanity.Text bag.Remove(c1.theID) bag.Add(c1.theID, c1) End Sub
Page Checkout.aspx Protected Sub Page_Load(. . .) Handles Me.Load Dim c1, c2 As Prog7_ShoppingItem Dim bag As SortedList = Session("Prog7_Bag") ‘ Need to find the form to add the control Dim theForm As Control = Master.Master.FindControl("form1") . . . theForm.Controls.Add(c1) End Sub
Page Checkout.aspx Protected Sub Page_Load(. . .) Handles Me.Load Dim c1, c2 As Prog7_ShoppingItem . . . ‘ New does not work here c1 = CType(LoadControl("../ShoppingItem.ascx"), Prog7_ShoppingItem) c2 = bag.GetByIndex(0) c1.theID = c2.theID c1.theQuantity = c2.theQuantity theForm.Controls.Add(c1) End Sub
Page Checkout.aspx Protected Sub Page_Load(. . .) Handles Me.Load Dim c1, c2 As Prog7_ShoppingItem . . . ‘ Must use a loop to add all items in bag For . . . c1 = CType(LoadControl("../ShoppingItem.ascx"), Prog7_ShoppingItem) c2 = bag.GetByIndex(index) c1.theID = c2.theID c1.theQuantity = c2.theQuantity theForm.Controls.Add(c1) Next End Sub
Page Checkout.aspx Protected Sub Page_Load(. . .) Handles Me.Load Dim c1, c2 As Prog7_ShoppingItem . . . ‘ Must use a loop to add all items in bag For . . . c1 = CType(LoadControl("../ShoppingItem.ascx"), Prog7_ShoppingItem) ‘ Can add c1 to any other control? theForm.Controls.Add(c1) Next End Sub