I have a webpage with a PlaceHolder and a Button. During the Load event of the page a dynamic CheckBoxList is created and added to the PlaceHolder.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim chkList As New CheckBoxList
chkList.Items.Add("Item 1")
chkList.Items.Add("Item 2")
chkList.Items.Add("Item 3")
chkList.Items.Add("Item 4")
chkList.Items.Add("Item 5")
PlaceHolder1.Controls.Add(chkList)
End Sub
A user can check some items in the CheckBoxList and then do a postback of the page by the button. During the Load Event of the page I want to know which items the user has checked.
For Each lst As ListItem In chkList.Items
If lst.Selected Then'TO DO
End If
Next
But every item has the selected property as False, but when the page is shown, the items the user has checked are still checked? I don't have this problem with a static CheckBoxList, but I can't use static CheckBoxLists because in reality there are several lists and I don't know how many there are until I load the page, so I have to make them dynamically. I fixed this now with a Session variable I added to the button click event which does the postback of the page, but I don't like this solution. I wonder if there is any better than this one.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If Session.Item("checked") IsNot Nothing Then
Session.Remove("checked")
End If
Dim myList As New System.Collections.Generic.List(Of String)
For Each chkList As CheckBoxList In PlaceHolder1.Controls
For Each lst As ListItem In chkList.Items
If lst.Selected Then
myList.Add(lst.Value)
End If
Next
Next
Session.Add("checked", myList)
End Sub
Fixed by adding my checkboxlists during the init event of my page, not the page load. Then during the page load of my page the selected values are correct when I loop through them.[:D]
I tested the previous post prior to posting. Therefore, you can do it in the page load. However, as per the example you must initialise the dynamic object regardless of postback state.
You could also make the current checkboxlist variable and array of checkboxlists. Then you can iterate through the specific array, without having to potentially iterate through additional controls in the your placeholder.
Rgds, Martin.
For the benefit of all users please mark any post answers as appropriate.
DarthDavy
Member
95 Points
23 Posts
Selected values from a dynamically created CheckBoxList
Sep 08, 2006 08:38 AM|LINK
Hi all,
I have a webpage with a PlaceHolder and a Button. During the Load event of the page a dynamic CheckBoxList is created and added to the PlaceHolder.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim chkList As New CheckBoxList chkList.Items.Add("Item 1") chkList.Items.Add("Item 2") chkList.Items.Add("Item 3") chkList.Items.Add("Item 4") chkList.Items.Add("Item 5") PlaceHolder1.Controls.Add(chkList) End SubA user can check some items in the CheckBoxList and then do a postback of the page by the button. During the Load Event of the page I want to know which items the user has checked.
But every item has the selected property as False, but when the page is shown, the items the user has checked are still checked? I don't have this problem with a static CheckBoxList, but I can't use static CheckBoxLists because in reality there are several lists and I don't know how many there are until I load the page, so I have to make them dynamically. I fixed this now with a Session variable I added to the button click event which does the postback of the page, but I don't like this solution. I wonder if there is any better than this one.
DarthDavy
Member
95 Points
23 Posts
Re: Selected values from a dynamically created CheckBoxList
Sep 08, 2006 12:21 PM|LINK
Damn it, it doesn't work with the session variable. The Click event is executed AFTER the page is loaded from the server, so it's too late... [:(]
I need a solution for this...[:'(]
mokeefe
Star
10850 Points
2098 Posts
Re: Selected values from a dynamically created CheckBoxList
Sep 08, 2006 01:37 PM|LINK
How about this.
Partial Class test
Inherits CyrilJohnston.CoreLibrary.WebPage
Dim cbxlist As New CheckBoxList
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
Dim sitems As New System.Collections.Specialized.StringCollection
cbxlist.ID = "dynamicCheckBox"
For i As Integer = 1 To 10
sitems.Add(i.ToString)
Next
cbxlist.DataSource = sitems
Me.PlaceHolder1.Controls.Add(cbxlist)
cbxlist.DataBind()
End Sub
………
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim CheckedIndexes As String = ""
Dim CheckedText As String = ""
For i As Integer = 0 To Me.cbxlist.Items.Count - 1
If Me.cbxlist.Items(i).Selected Then
CheckedIndexes &= "Index=" & Me.cbxlist.Items(i).Value & ControlChars.CrLf
End If
Next
' Breakpoint here to view
CheckedIndexes = ""
End Sub
Rgds,
Martin.
Martin.
For the benefit of all users please mark any post answers as appropriate.
DarthDavy
Member
95 Points
23 Posts
Re: Selected values from a dynamically created CheckBoxList
Sep 08, 2006 02:26 PM|LINK
mokeefe
Star
10850 Points
2098 Posts
Re: Selected values from a dynamically created CheckBoxList
Sep 09, 2006 12:10 AM|LINK
Hello,
I tested the previous post prior to posting. Therefore, you can do it in the page load. However, as per the example you must initialise the dynamic object regardless of postback state.
You could also make the current checkboxlist variable and array of checkboxlists. Then you can iterate through the specific array, without having to potentially iterate through additional controls in the your placeholder.
Martin.
For the benefit of all users please mark any post answers as appropriate.
DarthDavy
Member
95 Points
23 Posts
Re: Selected values from a dynamically created CheckBoxList
Sep 11, 2006 07:10 AM|LINK
dhaval950
Member
2 Points
2 Posts
Re: Selected values from a dynamically created CheckBoxList
Feb 08, 2008 04:28 AM|LINK
thank you very much for this solution
you know ,my most of load is finish from your solution
thank...