Re: Selecting items in a grid using checkboxes and getting a list of items selected..
It could be a databinding problem i.e. when the page posts back it is rebinding the data every time and you are trying to access the items when it has been cleared out. You really only want to call DataGrid.DataBind() when it isn't a postback or after you have updated some information and you want it to be show on screen.
Check that first let me know if it is still returning 0.
Re: Selecting items in a grid using checkboxes and getting a list of items selected...
Hmmm...I did have SetupGrid in the Page_load event.
Code:
Private Sub SetGridProperties()
With grdDockets
.AutoGenerateColumns = False
.AllowPaging = False
.AllowSorting = False
.PagerStyle.Mode = PagerMode.NextPrev
.PagerStyle.NextPageText = "Next"
.PagerStyle.PrevPageText = "Prev"
.PageSize = 10
End With
End Sub
However, I have now moved this into my BindDockets function that does only get called once.
So, when I have a postback, no grid props are being changed etc. However, it still says I have 0 rows
Ahhhh...not sure if this is a major issue, but my grid is in a user web control...this shouldn't make a difference should it? I bet it does
Re: Selecting items in a grid using checkboxes and getting a list of items selected...
OK. My usercontrol code is:
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Public Sub BindDockets(ByRef Dockets As GateKeeperElevator.Dockets, ByVal AllowSelect As Boolean)
SetGridProperties()
CreateColumns(AllowSelect)
grdDockets.DataSource = Dockets
grdDockets.DataBind()
End Sub
Private Sub CreateColumns(ByVal AllowSelect As Boolean)
Dim tc As TemplateColumn
grdDockets.Columns.Clear()
tc = New TemplateColumn
tc.HeaderTemplate = New DocketTemplate(ListItemType.Header, "Docket")
tc.ItemTemplate = New DocketTemplate(ListItemType.Item, "DocketNo")
grdDockets.Columns.Add(tc)
tc = New TemplateColumn
tc.HeaderTemplate = New ContractorTemplate(ListItemType.Header, "Contractor")
tc.ItemTemplate = New ContractorTemplate(ListItemType.Item, "Contractor")
grdDockets.Columns.Add(tc)
If AllowSelect Then
tc = New TemplateColumn
tc.HeaderTemplate = New SelectTemplate(ListItemType.Header, "")
tc.ItemTemplate = New SelectTemplate(ListItemType.Item, "")
grdDockets.Columns.Add(tc)
End If
End Sub
Private Sub SetGridProperties()
With grdDockets
.AutoGenerateColumns = False
.AllowPaging = False
.AllowSorting = False
.PagerStyle.Mode = PagerMode.NextPrev
.PagerStyle.NextPageText = "Next"
.PagerStyle.PrevPageText = "Prev"
.PageSize = 10
End With
End Sub
Public Function GetSelectedDocketNos() As String()
Dim DataItem As DataGridItem
Dim chk As CheckBox
Dim Dockets() As String
For Each DataItem In grdDockets.Items
chk = CType(DataItem.FindControl("chkSelected"), System.Web.UI.WebControls.CheckBox)
If chk.Checked Then
'change Dockets array
Dockets(Dockets.Length - 1) = CType(DataItem.FindControl("lblDocketNo"), System.Web.UI.WebControls.Label).Text
End If
Next
Return Dockets
End Function
End Class
I use template items to create the funky cell structures you see in the grid.
Here's the Template to Display the docket.
Code:
Public Class DocketTemplate
Implements ITemplate
Private TemplateType As ListItemType
Private ColumnHeading As String
Public Sub New(ByVal Type As ListItemType, ByVal Heading As String)
TemplateType = Type
ColumnHeading = Heading
End Sub
Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
Select Case TemplateType
Case ListItemType.Header
Dim lbl As New Label
lbl.Text = ColumnHeading
container.Controls.Add(lbl)
Case ListItemType.Item
Dim tbl As New Table
Dim Row As TableRow
Dim Cell As TableCell
Dim lbl As Label
Row = New TableRow
Cell = New TableCell
lbl = New Label
lbl.ID = "lblDocketNo"
lbl.Font.Size = New FontUnit(8)
lbl.Font.Bold = True
Cell.Controls.Add(lbl)
AddHandler lbl.DataBinding, AddressOf DocketNo_DataBinding
Row.Cells.Add(Cell)
tbl.Rows.Add(Row)
Row = New TableRow
Cell = New TableCell
lbl = New Label
lbl.Font.Size = New FontUnit(7)
lbl.ForeColor = Color.Gray
Cell.Controls.Add(lbl)
AddHandler lbl.DataBinding, AddressOf Status_DataBinding
Row.Cells.Add(Cell)
tbl.Rows.Add(Row)
Row = New TableRow
Cell = New TableCell
lbl = New Label
lbl.Font.Size = New FontUnit(7)
lbl.ForeColor = Color.Gray
Cell.Controls.Add(lbl)
AddHandler lbl.DataBinding, AddressOf Description_DataBinding
Row.Cells.Add(Cell)
tbl.Rows.Add(Row)
container.Controls.Add(tbl)
Cell = CType(container, TableCell)
Cell.VerticalAlign = VerticalAlign.Top
End Select
End Sub
Private Sub DocketNo_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lbl As Label
lbl = CType(sender, Label)
Dim container As DataGridItem
container = CType(lbl.NamingContainer, DataGridItem)
lbl.Text = CType(DataBinder.Eval(container.DataItem, "DocketNo"), String)
'lnk.NavigateUrl = "Docket.aspx?Key=" & CType(DataBinder.Eval(container.DataItem, "DocketNo"), String)
End Sub
Private Sub Description_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lbl As Label
lbl = CType(sender, Label)
Dim container As DataGridItem
container = CType(lbl.NamingContainer, DataGridItem)
lbl.Text = CType(DataBinder.Eval(container.DataItem, "Description"), String)
End Sub
Private Sub Status_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lbl As Label
lbl = CType(sender, Label)
Dim container As DataGridItem
container = CType(lbl.NamingContainer, DataGridItem)
lbl.Text = (New GateKeeperBR.DocketsBR).GetStatusDescription(CType(DataBinder.Eval(container.DataItem, "Status"), GateKeeperBR.DocketsBR.vbDocketStatusConstants))
End Sub
Private Sub ContractorName_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lbl As Label
lbl = CType(sender, Label)
Dim container As DataGridItem
container = CType(lbl.NamingContainer, DataGridItem)
lbl.Text = CType(DataBinder.Eval(container.DataItem, "ContractorName"), String)
End Sub
End Class
Re: Selecting items in a grid using checkboxes and getting a list of items selected...
No...that code doesn't get executed. I know there was an err with that code.
I have create a new project, with a new user control, with a new data grid...etc and this works.
Apparently its because I am dynamically adding columns
This sucks
Re: Selecting items in a grid using checkboxes and getting a list of items selected..
It shouldn't matter that you are dynamically adding columns the only problem is at what point you are adding them in comparison to when you are trying to access the cell contents.
Could you post the complete code for the page and the user control (including HTML part) as I'm having real trouble linking everything together in my head.