peterdidow
asked on
How to Maintain checkbox state across datagrid pages?
I am working on an eccomerce application which displays products in a datagrid. This datagrid is paged and one of the columns is a checkbox. The problem is I cant seem to keep the checkbox's state across datagrid pages. HELP! Here is what I have so far:
Code Behind:
---------------
Imports StoreFront.BusinessRule
Imports StoreFront.BusinessRule.Ma nagement
Imports StoreFront.SystemBase
Imports System.Text
Imports System.Web.UI.Page
Public MustInherit Class n_FeaturedProductAdmin
Inherits System.Web.UI.UserControl
Private Product1 As New SystemBase.NFeaturedProduc tBase
Private Product2 As New SystemBase.NFeaturedProduc tBase
Private Product3 As New SystemBase.NFeaturedProduc tBase
Private Product4 As New SystemBase.NFeaturedProduc tBase
Protected WithEvents dgProducts As System.Web.UI.WebControls. DataGrid
Protected WithEvents btnUpdate As System.Web.UI.WebControls. Button
Protected WithEvents lblMessage As System.Web.UI.WebControls. Label
Protected CheckBox As System.Web.UI.WebControls. CheckBox
Dim oSet As New SystemBase.NFeaturedProduc tBaseSet
Protected WithEvents Literal1 As System.Web.UI.WebControls. Literal
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
End Sub
Protected WithEvents Product4Description As System.Web.UI.WebControls. HyperLink
Protected WithEvents imgProduct4 As System.Web.UI.WebControls. Image
Protected WithEvents Product3Description As System.Web.UI.WebControls. HyperLink
Protected WithEvents imgProduct3 As System.Web.UI.WebControls. Image
Protected WithEvents Product2Description As System.Web.UI.WebControls. HyperLink
Protected WithEvents imgProduct2 As System.Web.UI.WebControls. Image
Protected WithEvents Product1Description As System.Web.UI.WebControls. HyperLink
Protected WithEvents imgProduct1 As System.Web.UI.WebControls. Image
Protected WithEvents Label1 As System.Web.UI.WebControls. Label
Protected WithEvents ddlCategory As System.Web.UI.WebControls. DropDownLi st
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara tion As System.Object
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
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
ddlCategory.SelectedIndex = 0
End If
Session("LandingPageCatego ryID") = 1
Dim CategoryID As Integer = Session("LandingPageCatego ryID")
GetFeaturedItems(CategoryI D)
GetProductDescriptionsETC( )
If Not Page.IsPostBack Then
GetTopLevelCategories()
End If
dgProducts.DataSource = CType(Session("datasource" ), DataSet)
dgProducts.DataBind()
End Sub
Private ReadOnly Property CompletedList() As Hashtable
Get
Dim hashtable As Hashtable
If IsNothing(ViewState("Compl eted")) Then
hashtable = New Hashtable
ViewState("Completed") = hashtable
Else
hashtable = CType(ViewState("Completed "), Hashtable)
End If
Return hashtable
End Get
End Property
Public Sub GetTopLevelCategories()
Dim CategoryMGR As New BusinessRule.CCategories
Dim oCategories As New SystemBase.CCategory
Dim oCatArray As ArrayList
oCatArray = CategoryMGR.ParantCategori es()
ddlCategory.DataSource = oCatArray.ToArray
ddlCategory.DataBind()
End Sub
Public Function GetProductID(ByVal FeaturedProductNumber As Integer) As Integer
Dim ProductID As Integer
Select Case FeaturedProductNumber
'Hard Coded for now, should get id from db
Case 1
ProductID = Product1.UID()
Case 2
ProductID = Product2.UID()
Case 3
ProductID = Product3.UID()
Case 4
ProductID = Product4.UID()
End Select
Return ProductID
End Function
Public Sub GetProductDescriptionsETC( )
Product1Description.Text = Product1.Name
Product1Description.Naviga teUrl = "http://servername/estore_tech/detail.aspx?ID=" & Product1.UID
imgProduct1.ImageUrl = Product1.ImageSmallPath
Product2Description.Text = Product2.Name
Product2Description.Naviga teUrl = "http://servername/estore_tech/detail.aspx?ID=" & Product2.UID
imgProduct2.ImageUrl = Product2.ImageSmallPath
Product3Description.Text = Product3.Name
Product3Description.Naviga teUrl = "http://servername/estore_tech/detail.aspx?ID=" & Product3.UID
imgProduct3.ImageUrl = Product3.ImageSmallPath
Product4Description.Text = Product4.Name
Product4Description.Naviga teUrl = "http://servername/estore_tech/detail.aspx?ID=" & Product4.UID
imgProduct4.ImageUrl = Product4.ImageSmallPath
End Sub
Public Sub GetFeaturedItems(ByVal CategoryID As Integer)
Dim FeaturedProductMGR As New BusinessRule.FeaturedProdu cts.NFeatu redProduct
Dim oFeaturedItem As New SystemBase.NFeaturedProduc tBase
oSet = FeaturedProductMGR.LoadFea turedProdu cts(Catego ryID)
Product1 = oSet(0)
Product2 = oSet(1)
Product3 = oSet(2)
Product4 = oSet(3)
End Sub
Private Sub ddlCategory_SelectedIndexC hanged(ByV al sender As System.Object, ByVal e As System.EventArgs) Handles ddlCategory.SelectedIndexC hanged
BindData(dgProducts.Curren tPageIndex )
End Sub
Public Sub NewPage(ByVal source As Object, ByVal e As System.Web.UI.WebControls. DataGridPa geChangedE ventArgs) Handles dgProducts.PageIndexChange d
ProcessCheckBoxes()
BindData(e.NewPageIndex)
dgProducts.CurrentPageInde x = e.NewPageIndex
End Sub
Public Sub BindData(ByVal val As Integer)
'Get the First Level Children for the selected Landing Page Category
Dim CategoryMGR As New BusinessRule.CCategories
Dim products As New FeaturedProducts.NFeatured Product
Dim c As New CXMLCategoryList
Dim oCatArray As ArrayList
Dim x As Integer
Dim oArray As ArrayList
oCatArray = CategoryMGR.GetFirstLevelC hildren(dd lCategory. SelectedVa lue)
'Get Products by Category for each of the child categories
For x = 0 To oCatArray.Count - 1
c = oCatArray(x)
oArray = CategoryMGR.GetAllProducts ByCategory (c.ID) 'the productid is returned.
Next
'the uid in the product table is the productid from above
' We need to grab the product data for each item in the arraylist (each product)
Dim ds As New DataSet
ds.Tables.Add("Products")
'GetProductData
ds = products.LoadProductsForCa tegories(o Array)
dgProducts.CurrentPageInde x = val
dgProducts.DataKeyField = "UID"
dgProducts.DataSource = ds
Session("datasource") = ds
dgProducts.DataBind()
End Sub
Private Sub ProcessCheckBoxes()
Dim checkBox As System.Web.UI.WebControls. CheckBox
Dim key As String
For Each item As DataGridItem In dgProducts.Items
Select Case item.ItemType
Case ListItemType.Item, ListItemType.AlternatingIt em
checkBox = CType(item.Controls(2).Fin dControl(" chkSelecte d"), System.Web.UI.WebControls. CheckBox)
If Not IsNothing(checkBox) Then
key = dgProducts.DataKeys(item.I temIndex). ToString()
If checkBox.Checked = True And (CompletedList.ContainsKey (key) = False Or CType(CompletedList(key), Boolean) = False) Then
CompletedList(key) = True
ElseIf checkBox.Checked = False And CompletedList.ContainsKey( key) = True And CType(CompletedList(key), Boolean) = True Then
CompletedList(key) = False
End If
End If
End Select
Next
End Sub
Public Function TODouble(ByVal val As String) As Double
Return FormatCurrency(val, 2, TriState.True, TriState.True, TriState.True)
End Function
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim sb As StringBuilder = New StringBuilder
ProcessCheckBoxes()
sb.Append("<table>")
sb.Append("<tr><th>Order Checked</th></tr>")
For Each entry As DictionaryEntry In CompletedList
If CType(entry.Value, Boolean) = True Then
sb.AppendFormat("<tr><td>{ 0}</td></t r>", entry.Key)
End If
Next
sb.Append("</table>")
Literal1.Text = sb.ToString()
'check if more than 4 have been Selected
Dim nCount As Integer
For Each dgItem As DataGridItem In dgProducts.Items
CheckBox = CType(dgItem.FindControl(" chkSelecte d"), CheckBox)
If CheckBox.Checked = True Then
nCount += 1
End If
If nCount >= 4 Then
lblMessage.Text = "You may only select 4 items to be featured"
Exit Sub
End If
Next
nCount = 0
'Update the featured items for the selected toplevel category
Dim products As New FeaturedProducts.NFeatured Product
Dim oFeaturedProduct As New SystemBase.NFeaturedProduc tBase
For Each dgItem As DataGridItem In dgProducts.Items
CheckBox = CType(dgItem.FindControl(" chkSelecte d"), CheckBox)
If CheckBox.Checked = True Then
nCount += 1
If nCount <= 4 Then
Dim FeaturedProductMGR As New BusinessRule.FeaturedProdu cts.NFeatu redProduct
Dim oFeaturedItem As New SystemBase.NFeaturedProduc tBase
oFeaturedItem.CategoryID = ddlCategory.SelectedValue
oFeaturedItem.FeaturedProd uctNumber = nCount
oFeaturedItem.ImageSmallPa th = CType(dgItem.Cells(5).Text , String)
oFeaturedItem.Name = CType(dgItem.Cells(1).Text , String)
oFeaturedItem.UID = CType(dgItem.Cells(0).Text , Integer)
FeaturedProductMGR.SaveFea turedProdu cts(oFeatu redItem)
End If
End If
Next
End Sub
End Class
and the client side page:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="n_FeaturedProd uctAdmin.a scx.vb" Inherits="StoreFront.Store Front.n_Fe aturedProd uctAdmin" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<TABLE id="Table1" style="WIDTH: 736px; HEIGHT: 296px" width="736" border="1">
<TR>
<TD vAlign="top" width="20%"><asp:label id="Label1" Width="208px" runat="server">Select A Landing Page Category</asp:label><asp:d ropdownlis t id="ddlCategory" Width="208px" runat="server" AutoPostBack="True" DataValueField="ID"
DataTextField="Name"></asp :dropdownl ist></TD>
<TD vAlign="bottom" align="right" width="30%" colSpan="2" rowSpan="1"><asp:datagrid id="dgProducts" Width="568px" runat="server" AutoGenerateColumns="False " PageSize="10"
AllowPaging="True" OnPageIndexChanged="NewPag e" DataKeyField="UID" PagerStyle-Mode="NumericPa ges" EnableViewState=True>
<Columns>
<asp:TemplateColumn HeaderText="Selected">
<ItemTemplate>
<asp:CheckBox ID="chkSelected" Runat="server" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn Visible="False" DataField="UID"></asp:Boun dColumn>
<asp:BoundColumn DataField="Name" HeaderText="Name" ItemStyle-Width="320px"></ asp:BoundC olumn>
<asp:BoundColumn DataField="Code" HeaderText="Code" ItemStyle-Width="60px"></a sp:BoundCo lumn>
<asp:BoundColumn DataField="Price" HeaderText="Price" DataFormatString="{0:C}">< /asp:Bound Column>
<asp:BoundColumn DataField="Cost" HeaderText="Cost" DataFormatString="{0:C}">< /asp:Bound Column>
<asp:TemplateColumn HeaderText="Image">
<ItemTemplate>
<img src='http://almira/estore_tech/<%# DataBinder.Eval(Container. DataItem," ImageSmall Path") %>' border=0 id="ThumbImg">
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle PageButtonCount="10"></Pag erStyle>
</asp:datagrid><asp:button id="btnUpdate" runat="server" Text="Update Featured Products"></asp:button></T D>
<TD vAlign="top" align="center" width="30%"></TD>
<TD width="20%"></TD>
</TR>
<TR>
<TD vAlign="top" width="20%"></TD>
<TD vAlign="bottom" align="center" width="30%" colSpan="2"> Featured Products
for the Selected Category</TD>
<TD vAlign="top" align="center" width="30%"></TD>
<TD width="20%"></TD>
</TR>
<TR>
<TD width="20%"></TD>
<TD vAlign="top" align="center" width="30%"><A
href="http://servername/estore_tech/detail.aspx?ID=<%=GetProductID(1)%> "
><asp:image id="imgProduct1" runat="server"></asp:image ></A><BR>
<asp:hyperlink id="Product1Description" Runat="server"></asp:hyper link><BR>
</TD>
<TD vAlign="top" align="center" width="30%"><A
href="http://servername/estore_tech/detail.aspx?ID=<%=GetProductID(2)%> "
><asp:image id="imgProduct2" runat="server"></asp:image > </A >
<BR>
<asp:hyperlink id="Product2Description" runat="server"></asp:hyper link><BR>
</TD>
<TD width="20%"></TD>
</TR>
<TR>
<TD width="20%"></TD>
<TD vAlign="top" align="center" width="30%"><A
href="http://servername/estore_tech/detail.aspx?ID=<%=GetProductID(3)%> "
><asp:image id="imgProduct3" runat="server"></asp:image > </A >
<BR>
<asp:hyperlink id="Product3Description" runat="server"></asp:hyper link><BR>
</TD>
<BR>
<TD vAlign="top" align="center" width="30%"><A
href="http://servername/estore_tech/detail.aspx?ID=<%=GetProductID(4)%> "
><asp:image id="imgProduct4" runat="server"></asp:image > </A >
<BR>
<asp:hyperlink id="Product4Description" runat="server"></asp:hyper link><BR>
</TD>
<TD width="20%"></TD>
</TR>
</TABLE>
<P></P>
<asp:label id="lblMessage" runat="server"></asp:label >
Code Behind:
---------------
Imports StoreFront.BusinessRule
Imports StoreFront.BusinessRule.Ma
Imports StoreFront.SystemBase
Imports System.Text
Imports System.Web.UI.Page
Public MustInherit Class n_FeaturedProductAdmin
Inherits System.Web.UI.UserControl
Private Product1 As New SystemBase.NFeaturedProduc
Private Product2 As New SystemBase.NFeaturedProduc
Private Product3 As New SystemBase.NFeaturedProduc
Private Product4 As New SystemBase.NFeaturedProduc
Protected WithEvents dgProducts As System.Web.UI.WebControls.
Protected WithEvents btnUpdate As System.Web.UI.WebControls.
Protected WithEvents lblMessage As System.Web.UI.WebControls.
Protected CheckBox As System.Web.UI.WebControls.
Dim oSet As New SystemBase.NFeaturedProduc
Protected WithEvents Literal1 As System.Web.UI.WebControls.
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg
End Sub
Protected WithEvents Product4Description As System.Web.UI.WebControls.
Protected WithEvents imgProduct4 As System.Web.UI.WebControls.
Protected WithEvents Product3Description As System.Web.UI.WebControls.
Protected WithEvents imgProduct3 As System.Web.UI.WebControls.
Protected WithEvents Product2Description As System.Web.UI.WebControls.
Protected WithEvents imgProduct2 As System.Web.UI.WebControls.
Protected WithEvents Product1Description As System.Web.UI.WebControls.
Protected WithEvents imgProduct1 As System.Web.UI.WebControls.
Protected WithEvents Label1 As System.Web.UI.WebControls.
Protected WithEvents ddlCategory As System.Web.UI.WebControls.
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara
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
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
ddlCategory.SelectedIndex = 0
End If
Session("LandingPageCatego
Dim CategoryID As Integer = Session("LandingPageCatego
GetFeaturedItems(CategoryI
GetProductDescriptionsETC(
If Not Page.IsPostBack Then
GetTopLevelCategories()
End If
dgProducts.DataSource = CType(Session("datasource"
dgProducts.DataBind()
End Sub
Private ReadOnly Property CompletedList() As Hashtable
Get
Dim hashtable As Hashtable
If IsNothing(ViewState("Compl
hashtable = New Hashtable
ViewState("Completed") = hashtable
Else
hashtable = CType(ViewState("Completed
End If
Return hashtable
End Get
End Property
Public Sub GetTopLevelCategories()
Dim CategoryMGR As New BusinessRule.CCategories
Dim oCategories As New SystemBase.CCategory
Dim oCatArray As ArrayList
oCatArray = CategoryMGR.ParantCategori
ddlCategory.DataSource = oCatArray.ToArray
ddlCategory.DataBind()
End Sub
Public Function GetProductID(ByVal FeaturedProductNumber As Integer) As Integer
Dim ProductID As Integer
Select Case FeaturedProductNumber
'Hard Coded for now, should get id from db
Case 1
ProductID = Product1.UID()
Case 2
ProductID = Product2.UID()
Case 3
ProductID = Product3.UID()
Case 4
ProductID = Product4.UID()
End Select
Return ProductID
End Function
Public Sub GetProductDescriptionsETC(
Product1Description.Text = Product1.Name
Product1Description.Naviga
imgProduct1.ImageUrl = Product1.ImageSmallPath
Product2Description.Text = Product2.Name
Product2Description.Naviga
imgProduct2.ImageUrl = Product2.ImageSmallPath
Product3Description.Text = Product3.Name
Product3Description.Naviga
imgProduct3.ImageUrl = Product3.ImageSmallPath
Product4Description.Text = Product4.Name
Product4Description.Naviga
imgProduct4.ImageUrl = Product4.ImageSmallPath
End Sub
Public Sub GetFeaturedItems(ByVal CategoryID As Integer)
Dim FeaturedProductMGR As New BusinessRule.FeaturedProdu
Dim oFeaturedItem As New SystemBase.NFeaturedProduc
oSet = FeaturedProductMGR.LoadFea
Product1 = oSet(0)
Product2 = oSet(1)
Product3 = oSet(2)
Product4 = oSet(3)
End Sub
Private Sub ddlCategory_SelectedIndexC
BindData(dgProducts.Curren
End Sub
Public Sub NewPage(ByVal source As Object, ByVal e As System.Web.UI.WebControls.
ProcessCheckBoxes()
BindData(e.NewPageIndex)
dgProducts.CurrentPageInde
End Sub
Public Sub BindData(ByVal val As Integer)
'Get the First Level Children for the selected Landing Page Category
Dim CategoryMGR As New BusinessRule.CCategories
Dim products As New FeaturedProducts.NFeatured
Dim c As New CXMLCategoryList
Dim oCatArray As ArrayList
Dim x As Integer
Dim oArray As ArrayList
oCatArray = CategoryMGR.GetFirstLevelC
'Get Products by Category for each of the child categories
For x = 0 To oCatArray.Count - 1
c = oCatArray(x)
oArray = CategoryMGR.GetAllProducts
Next
'the uid in the product table is the productid from above
' We need to grab the product data for each item in the arraylist (each product)
Dim ds As New DataSet
ds.Tables.Add("Products")
'GetProductData
ds = products.LoadProductsForCa
dgProducts.CurrentPageInde
dgProducts.DataKeyField = "UID"
dgProducts.DataSource = ds
Session("datasource") = ds
dgProducts.DataBind()
End Sub
Private Sub ProcessCheckBoxes()
Dim checkBox As System.Web.UI.WebControls.
Dim key As String
For Each item As DataGridItem In dgProducts.Items
Select Case item.ItemType
Case ListItemType.Item, ListItemType.AlternatingIt
checkBox = CType(item.Controls(2).Fin
If Not IsNothing(checkBox) Then
key = dgProducts.DataKeys(item.I
If checkBox.Checked = True And (CompletedList.ContainsKey
CompletedList(key) = True
ElseIf checkBox.Checked = False And CompletedList.ContainsKey(
CompletedList(key) = False
End If
End If
End Select
Next
End Sub
Public Function TODouble(ByVal val As String) As Double
Return FormatCurrency(val, 2, TriState.True, TriState.True, TriState.True)
End Function
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim sb As StringBuilder = New StringBuilder
ProcessCheckBoxes()
sb.Append("<table>")
sb.Append("<tr><th>Order Checked</th></tr>")
For Each entry As DictionaryEntry In CompletedList
If CType(entry.Value, Boolean) = True Then
sb.AppendFormat("<tr><td>{
End If
Next
sb.Append("</table>")
Literal1.Text = sb.ToString()
'check if more than 4 have been Selected
Dim nCount As Integer
For Each dgItem As DataGridItem In dgProducts.Items
CheckBox = CType(dgItem.FindControl("
If CheckBox.Checked = True Then
nCount += 1
End If
If nCount >= 4 Then
lblMessage.Text = "You may only select 4 items to be featured"
Exit Sub
End If
Next
nCount = 0
'Update the featured items for the selected toplevel category
Dim products As New FeaturedProducts.NFeatured
Dim oFeaturedProduct As New SystemBase.NFeaturedProduc
For Each dgItem As DataGridItem In dgProducts.Items
CheckBox = CType(dgItem.FindControl("
If CheckBox.Checked = True Then
nCount += 1
If nCount <= 4 Then
Dim FeaturedProductMGR As New BusinessRule.FeaturedProdu
Dim oFeaturedItem As New SystemBase.NFeaturedProduc
oFeaturedItem.CategoryID = ddlCategory.SelectedValue
oFeaturedItem.FeaturedProd
oFeaturedItem.ImageSmallPa
oFeaturedItem.Name = CType(dgItem.Cells(1).Text
oFeaturedItem.UID = CType(dgItem.Cells(0).Text
FeaturedProductMGR.SaveFea
End If
End If
Next
End Sub
End Class
and the client side page:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="n_FeaturedProd
<TABLE id="Table1" style="WIDTH: 736px; HEIGHT: 296px" width="736" border="1">
<TR>
<TD vAlign="top" width="20%"><asp:label id="Label1" Width="208px" runat="server">Select A Landing Page Category</asp:label><asp:d
DataTextField="Name"></asp
<TD vAlign="bottom" align="right" width="30%" colSpan="2" rowSpan="1"><asp:datagrid id="dgProducts" Width="568px" runat="server" AutoGenerateColumns="False
AllowPaging="True" OnPageIndexChanged="NewPag
<Columns>
<asp:TemplateColumn HeaderText="Selected">
<ItemTemplate>
<asp:CheckBox ID="chkSelected" Runat="server" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn Visible="False" DataField="UID"></asp:Boun
<asp:BoundColumn DataField="Name" HeaderText="Name" ItemStyle-Width="320px"></
<asp:BoundColumn DataField="Code" HeaderText="Code" ItemStyle-Width="60px"></a
<asp:BoundColumn DataField="Price" HeaderText="Price" DataFormatString="{0:C}"><
<asp:BoundColumn DataField="Cost" HeaderText="Cost" DataFormatString="{0:C}"><
<asp:TemplateColumn HeaderText="Image">
<ItemTemplate>
<img src='http://almira/estore_tech/<%# DataBinder.Eval(Container.
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle PageButtonCount="10"></Pag
</asp:datagrid><asp:button
<TD vAlign="top" align="center" width="30%"></TD>
<TD width="20%"></TD>
</TR>
<TR>
<TD vAlign="top" width="20%"></TD>
<TD vAlign="bottom" align="center" width="30%" colSpan="2"> Featured
for the Selected Category</TD>
<TD vAlign="top" align="center" width="30%"></TD>
<TD width="20%"></TD>
</TR>
<TR>
<TD width="20%"></TD>
<TD vAlign="top" align="center" width="30%"><A
href="http://servername/estore_tech/detail.aspx?ID=<%=GetProductID(1)%>
><asp:image id="imgProduct1" runat="server"></asp:image
<asp:hyperlink id="Product1Description" Runat="server"></asp:hyper
</TD>
<TD vAlign="top" align="center" width="30%"><A
href="http://servername/estore_tech/detail.aspx?ID=<%=GetProductID(2)%>
><asp:image id="imgProduct2" runat="server"></asp:image
<BR>
<asp:hyperlink id="Product2Description" runat="server"></asp:hyper
</TD>
<TD width="20%"></TD>
</TR>
<TR>
<TD width="20%"></TD>
<TD vAlign="top" align="center" width="30%"><A
href="http://servername/estore_tech/detail.aspx?ID=<%=GetProductID(3)%>
><asp:image id="imgProduct3" runat="server"></asp:image
<BR>
<asp:hyperlink id="Product3Description" runat="server"></asp:hyper
</TD>
<BR>
<TD vAlign="top" align="center" width="30%"><A
href="http://servername/estore_tech/detail.aspx?ID=<%=GetProductID(4)%>
><asp:image id="imgProduct4" runat="server"></asp:image
<BR>
<asp:hyperlink id="Product4Description" runat="server"></asp:hyper
</TD>
<TD width="20%"></TD>
</TR>
</TABLE>
<P></P>
<asp:label id="lblMessage" runat="server"></asp:label
That is too much code. Where is the check box column defined?
Bob
Bob
ASKER
here is the code that actually pertains to the datagrid/checkbox.
Private Sub dgProducts_ItemDataBound(B yVal sender As Object, ByVal e As System.Web.UI.WebControls. DataGridIt emEventArg s) Handles dgProducts.ItemDataBound
Dim checkBox As System.Web.UI.WebControls. CheckBox
Dim key As String
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingIt em
checkBox = CType(e.Item.Controls(0).F indControl ("Complete dCheckBox" ), System.Web.UI.WebControls. CheckBox)
If Not IsNothing(checkBox) Then
key = dgProducts.DataKeys(e.Item .ItemIndex ).ToString ()
If Not CompletedList.ContainsKey( key) Then
checkBox.Checked = False
Else
checkBox.Checked = CType(CompletedList(key), Boolean)
End If
End If
End Select
'key = dgProducts.DataKeys(e.Item .ItemIndex ).ToString ()
'If dgProducts.DataKeyField = CompletedList(key) Then
' 'check it true
'Else
' 'unchecked.
'End If
End Sub
Private Sub ProcessCheckBoxes()
Dim checkBox As New System.Web.UI.WebControls. CheckBox
Dim key As String
For Each item As DataGridItem In dgProducts.Items
Select Case item.ItemType
Case ListItemType.Item, ListItemType.AlternatingIt em
checkBox = CType(item.Controls(0).Fin dControl(" chkSelecte d"), System.Web.UI.WebControls. CheckBox)
If Not IsNothing(checkBox) Then
key = dgProducts.DataKeys(item.I temIndex). ToString()
If checkBox.Checked = True And (CompletedList.ContainsKey (key) = False Or CType(CompletedList(key), Boolean) = False) Then
CompletedList(key) = True
ElseIf checkBox.Checked = False And CompletedList.ContainsKey( key) = True And CType(CompletedList(key), Boolean) = True Then
CompletedList(key) = False
End If
End If
End Select
Next
End Sub
Private ReadOnly Property CompletedList() As Hashtable
Get
Dim hashtable As hashtable
If IsNothing(ViewState("Compl eted")) Then
hashtable = New hashtable
ViewState("Completed") = hashtable
Else
hashtable = CType(ViewState("Completed "), hashtable)
End If
Return hashtable
End Get
End Property
Public Sub BindData(ByVal val As Integer)
'Get the First Level Children for the selected Landing Page Category
Dim CategoryMGR As New BusinessRule.CCategories
Dim products As New FeaturedProducts.NFeatured Product
Dim c As New CXMLCategoryList
Dim oCatArray As ArrayList
Dim x As Integer
Dim oArray As ArrayList
oCatArray = CategoryMGR.GetFirstLevelC hildren(dd lCategory. SelectedVa lue)
'Get Products by Category for each of the child categories
For x = 0 To oCatArray.Count - 1
c = oCatArray(x)
oArray = CategoryMGR.GetAllProducts ByCategory (c.ID) 'the productid is returned.
Next
'the uid in the product table is the productid from above
' We need to grab the product data for each item in the arraylist (each product)
'GetProductData
dgProducts.CurrentPageInde x = val
If Not Page.IsPostBack Then
Dim ds As New DataSet
ds = products.LoadProductsForCa tegories(o Array)
dgProducts.DataKeyField = "UID"
dgProducts.DataSource = ds.Tables(0).DefaultView
Session("DS") = ds
dgProducts.DataBind()
Else
If Not Session("DS") Is Nothing Then
dgProducts.DataSource = Session("DS")
dgProducts.DataBind()
Else
Dim ds As New DataSet
ds = products.LoadProductsForCa tegories(o Array)
dgProducts.DataKeyField = "UID"
dgProducts.DataSource = ds.Tables(0).DefaultView
Session("DS") = ds
dgProducts.DataBind()
End If
End If
End Sub
Public Sub NewPage(ByVal source As Object, ByVal e As System.Web.UI.WebControls. DataGridPa geChangedE ventArgs) Handles dgProducts.PageIndexChange d
ProcessCheckBoxes()
BindData(e.NewPageIndex)
dgProducts.CurrentPageInde x = e.NewPageIndex
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim sb As StringBuilder = New StringBuilder
ProcessCheckBoxes()
sb.Append("<table>")
sb.Append("<tr><th>Order Checked</th></tr>")
For Each entry As DictionaryEntry In CompletedList
If CType(entry.Value, Boolean) = True Then
sb.AppendFormat("<tr><td>{ 0}</td></t r>", entry.Key)
End If
Next
sb.Append("</table>")
literal1.Text = sb.ToString()
'check if more than 4 have been Selected
Dim nCount As Integer
For Each dgItem As DataGridItem In dgProducts.Items
CheckBox = CType(dgItem.FindControl(" chkSelecte d"), CheckBox)
If CheckBox.Checked = True Then
nCount += 1
End If
If nCount >= 4 Then
lblMessage.Text = "You may only select 4 items to be featured"
Exit Sub
End If
Next
nCount = 0
'Update the featured items for the selected toplevel category
Dim products As New FeaturedProducts.NFeatured Product
Dim oFeaturedProduct As New SystemBase.NFeaturedProduc tBase
For Each dgItem As DataGridItem In dgProducts.Items
CheckBox = CType(dgItem.FindControl(" chkSelecte d"), CheckBox)
If CheckBox.Checked = True Then
nCount += 1
If nCount <= 4 Then
Dim FeaturedProductMGR As New BusinessRule.FeaturedProdu cts.NFeatu redProduct
Dim oFeaturedItem As New SystemBase.NFeaturedProduc tBase
oFeaturedItem.CategoryID = ddlCategory.SelectedValue
oFeaturedItem.FeaturedProd uctNumber = nCount
oFeaturedItem.ImageSmallPa th = CType(dgItem.Cells(5).Text , String)
oFeaturedItem.Name = CType(dgItem.Cells(1).Text , String)
oFeaturedItem.UID = CType(dgItem.Cells(0).Text , Integer)
FeaturedProductMGR.SaveFea turedProdu cts(oFeatu redItem)
End If
End If
Next
End Sub
Private Sub dgProducts_ItemDataBound(B
Dim checkBox As System.Web.UI.WebControls.
Dim key As String
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingIt
checkBox = CType(e.Item.Controls(0).F
If Not IsNothing(checkBox) Then
key = dgProducts.DataKeys(e.Item
If Not CompletedList.ContainsKey(
checkBox.Checked = False
Else
checkBox.Checked = CType(CompletedList(key), Boolean)
End If
End If
End Select
'key = dgProducts.DataKeys(e.Item
'If dgProducts.DataKeyField = CompletedList(key) Then
' 'check it true
'Else
' 'unchecked.
'End If
End Sub
Private Sub ProcessCheckBoxes()
Dim checkBox As New System.Web.UI.WebControls.
Dim key As String
For Each item As DataGridItem In dgProducts.Items
Select Case item.ItemType
Case ListItemType.Item, ListItemType.AlternatingIt
checkBox = CType(item.Controls(0).Fin
If Not IsNothing(checkBox) Then
key = dgProducts.DataKeys(item.I
If checkBox.Checked = True And (CompletedList.ContainsKey
CompletedList(key) = True
ElseIf checkBox.Checked = False And CompletedList.ContainsKey(
CompletedList(key) = False
End If
End If
End Select
Next
End Sub
Private ReadOnly Property CompletedList() As Hashtable
Get
Dim hashtable As hashtable
If IsNothing(ViewState("Compl
hashtable = New hashtable
ViewState("Completed") = hashtable
Else
hashtable = CType(ViewState("Completed
End If
Return hashtable
End Get
End Property
Public Sub BindData(ByVal val As Integer)
'Get the First Level Children for the selected Landing Page Category
Dim CategoryMGR As New BusinessRule.CCategories
Dim products As New FeaturedProducts.NFeatured
Dim c As New CXMLCategoryList
Dim oCatArray As ArrayList
Dim x As Integer
Dim oArray As ArrayList
oCatArray = CategoryMGR.GetFirstLevelC
'Get Products by Category for each of the child categories
For x = 0 To oCatArray.Count - 1
c = oCatArray(x)
oArray = CategoryMGR.GetAllProducts
Next
'the uid in the product table is the productid from above
' We need to grab the product data for each item in the arraylist (each product)
'GetProductData
dgProducts.CurrentPageInde
If Not Page.IsPostBack Then
Dim ds As New DataSet
ds = products.LoadProductsForCa
dgProducts.DataKeyField = "UID"
dgProducts.DataSource = ds.Tables(0).DefaultView
Session("DS") = ds
dgProducts.DataBind()
Else
If Not Session("DS") Is Nothing Then
dgProducts.DataSource = Session("DS")
dgProducts.DataBind()
Else
Dim ds As New DataSet
ds = products.LoadProductsForCa
dgProducts.DataKeyField = "UID"
dgProducts.DataSource = ds.Tables(0).DefaultView
Session("DS") = ds
dgProducts.DataBind()
End If
End If
End Sub
Public Sub NewPage(ByVal source As Object, ByVal e As System.Web.UI.WebControls.
ProcessCheckBoxes()
BindData(e.NewPageIndex)
dgProducts.CurrentPageInde
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim sb As StringBuilder = New StringBuilder
ProcessCheckBoxes()
sb.Append("<table>")
sb.Append("<tr><th>Order Checked</th></tr>")
For Each entry As DictionaryEntry In CompletedList
If CType(entry.Value, Boolean) = True Then
sb.AppendFormat("<tr><td>{
End If
Next
sb.Append("</table>")
literal1.Text = sb.ToString()
'check if more than 4 have been Selected
Dim nCount As Integer
For Each dgItem As DataGridItem In dgProducts.Items
CheckBox = CType(dgItem.FindControl("
If CheckBox.Checked = True Then
nCount += 1
End If
If nCount >= 4 Then
lblMessage.Text = "You may only select 4 items to be featured"
Exit Sub
End If
Next
nCount = 0
'Update the featured items for the selected toplevel category
Dim products As New FeaturedProducts.NFeatured
Dim oFeaturedProduct As New SystemBase.NFeaturedProduc
For Each dgItem As DataGridItem In dgProducts.Items
CheckBox = CType(dgItem.FindControl("
If CheckBox.Checked = True Then
nCount += 1
If nCount <= 4 Then
Dim FeaturedProductMGR As New BusinessRule.FeaturedProdu
Dim oFeaturedItem As New SystemBase.NFeaturedProduc
oFeaturedItem.CategoryID = ddlCategory.SelectedValue
oFeaturedItem.FeaturedProd
oFeaturedItem.ImageSmallPa
oFeaturedItem.Name = CType(dgItem.Cells(1).Text
oFeaturedItem.UID = CType(dgItem.Cells(0).Text
FeaturedProductMGR.SaveFea
End If
End If
Next
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER