Imports BVSoftware.BVC.Core Public Class products_products_edit_categories Inherits BaseAdminPage Protected WithEvents lblProductName As System.Web.UI.WebControls.Label Protected WithEvents MetaDescriptionControl As BVSoftware.WebControls.MetaTag Protected WithEvents MetaKeywordsControl As BVSoftware.WebControls.MetaTag Protected WithEvents CancelButton As System.Web.UI.WebControls.ImageButton Protected WithEvents SaveButton As System.Web.UI.WebControls.ImageButton Protected WithEvents WebPageMessage1 As BVSoftware.WebControls.WebPageMessage Protected WithEvents msg As BVSoftware.WebControls.WebPageMessage Protected WithEvents dgCategories As System.Web.UI.WebControls.DataGrid Protected WithEvents producttabs As AdminProductTabs #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles MyBase.Load BVC2004Store.CheckThisPage(Security.RolePermission.LoginToAdmin) BVC2004Store.CheckThisPage(Security.RolePermission.AccessProductArea) If Not Page.IsPostBack Then MetaKeywordsControl.Content = WebAppSettings.MetaKeywords MetaDescriptionControl.Content = WebAppSettings.MetaDescription End If If Not Page.IsPostBack() Then Dim EditID As String EditID = Request.Params("id") If EditID.Trim.Length < 1 Then msg.ShowError("Unable to load the requested product.") Else Dim p As New Catalog.Product p = CatalogServices.Products.GetProduct(EditID, True) If Not p Is Nothing Then ViewState("ID") = EditID lblProductName.Text = p.ID & " " & p.ProductName Else msg.ShowError("Unable to load the requested product.") End If p = Nothing End If LoadCategories() End If End Sub Sub LoadCategories() Dim currentCategories As DataTable currentCategories = CatalogServices.Products.GetCategories(ViewState("ID")) Dim allCategories As DataTable = CatalogServices.Categories.GetFullCategoryTree allCategories.Columns.Add("Original", System.Type.GetType("System.Int32")) For i As Integer = 0 To allCategories.Rows.Count - 1 allCategories.Rows(i).Item("Original") = 0 For j As Integer = 0 To currentCategories.Rows.Count - 1 If currentCategories.Rows(j).Item("ID") = allCategories.Rows(i).Item("ID") Then allCategories.Rows(i).Item("Original") = 1 End If Next Next Me.dgCategories.DataSource = allCategories Me.dgCategories.DataBind() End Sub Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles CancelButton.Click Cancel() End Sub Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles SaveButton.Click Save() End Sub Private Sub Cancel() Response.Redirect("products_Products_edit.aspx?id=" & ViewState("ID")) End Sub Private Sub Save() msg.Clear() SaveSettings() LoadCategories() msg.ShowOK("Changes Saved!") End Sub Private Sub SaveSettings() For Each di As DataGridItem In Me.dgCategories.Items If di.ItemType = ListItemType.AlternatingItem Or di.ItemType = ListItemType.Item Then Dim chk1 As CheckBox = di.FindControl("chkSelected") Dim original As Boolean = False If di.Cells(2).Text = "1" Then original = True End If If Not chk1 Is Nothing Then If chk1.Checked <> original Then Dim p As New Catalog.Product p = CatalogServices.Products.GetProduct(ViewState("ID")) If Not p Is Nothing Then If chk1.Checked = True Then CatalogServices.Products.AddCategoryToProduct(p.ID, dgCategories.DataKeys(di.ItemIndex)) Else CatalogServices.Products.RemoveCategoryFromProduct(p.ID, dgCategories.DataKeys(di.ItemIndex)) End If End If p = Nothing End If End If End If Next End Sub Private Sub dgCategories_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgCategories.ItemDataBound If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then Dim chk1 As CheckBox = e.Item.FindControl("chkSelected") If Not chk1 Is Nothing Then If e.Item.DataItem("Original") = 1 Then chk1.Checked = True Else chk1.Checked = False End If End If Dim depth As Integer = e.Item.DataItem("depth") For i As Integer = 0 To depth e.Item.Cells(1).Text = "   " & e.Item.Cells(1).Text Next depth = Nothing End If End Sub Sub TabButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles producttabs.ButtonClicked Me.SaveSettings() End Sub End Class