Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 3870/CS 5870 Web User Controls.

Similar presentations


Presentation on theme: "CS 3870/CS 5870 Web User Controls."— Presentation transcript:

1 CS 3870/CS 5870 Web User Controls

2 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

3 Creating ShoppingItem.ascx
For page Checkout Add New Items Web User Control File name ShoppingItem.ascx Place code in separate file Different file extension

4 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

5 Setting Properties of Controls
Read Only Width Alignment Using Style

6 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

7 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

8 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

9 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

10 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

11 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

12 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>

13 Page Default.aspx Protected Sub Page_Load(. . .) Handles Me.Load ShoppingItem.theID = "101" ShoppingItem.theQuantity = 20 End Sub

14 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" %>

15 Shopping Bag Use SortedList

16 Global.asax Session("Prog7_Bag") = New SortedList

17 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

18 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

19 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

20 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

21 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

22 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


Download ppt "CS 3870/CS 5870 Web User Controls."

Similar presentations


Ads by Google