473,466 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamic CheckBoxList???

Using VS.Net 2k3 to build ASP.Net app with VB code-behind pages...

Hi, all! I've been struggling with getting a dynamically-generated
CheckBoxList generated. I've finally been able to get the list
generated, but now there are two problems I haven't been able to
overcome:

1) ASP.Net is munging the checkbox ID/Names of the checkboxes: I give
it a name like "myCheckbox" and asp.net is creating the checkboxes
with the name myCheckbox_1,myCheckBox_2, etc...
2) I've tried iterating over the submitted form to get the field
values, but to no avail.

What's the deal? *sigh* All I want is a list of
dynamically-generated checkboxes, each with the same name (ala old
HTML style checkboxes which render a comma-delimited list of values.
Help!! :)

Jack

Nov 19 '05 #1
3 5703
Hi Jack,

Try this:

DynamicCheckBoxes.aspx
----------------
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<br>
<asp:Literal id="mySelections" Runat="server"></asp:Literal>
<asp:PlaceHolder ID="checkboxContainer" Runat="server" />
<br>
<asp:Button ID="run" Text="Submit" Runat="server" />
</form>
</body>

DynamicCheckBoxes.aspx.vb
-----------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

Dim userSelectionList As New CheckBoxList
userSelectionList.ID = "mylist"
userSelectionList.Items.Add(New ListItem("Value #1", "1"))
userSelectionList.Items.Add(New ListItem("Value #2", "2"))
userSelectionList.Items.Add(New ListItem("Value #3", "3"))

Dim selectedItem As New ListItem("Value #4 Selected", "4")
selectedItem.Selected = True
userSelectionList.Items.Add(selectedItem)

checkboxContainer.Controls.Add(userSelectionList)

End Sub

Private Sub run_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles run.Click
Dim userSelectionList As CheckBoxList = CType(FindControl("mylist"),
CheckBoxList)

Dim x As Integer
For x = 0 To userSelectionList.Items.Count - 1
If userSelectionList.Items(x).Selected = True Then
mySelections.Text += userSelectionList.Items(x).Value
End If

Next

End Sub
Hope that helps,
Shane

"Jack Black" <ja********@hotmail.com> wrote in message
news:il********************************@4ax.com...
Using VS.Net 2k3 to build ASP.Net app with VB code-behind pages...

Hi, all! I've been struggling with getting a dynamically-generated
CheckBoxList generated. I've finally been able to get the list
generated, but now there are two problems I haven't been able to
overcome:

1) ASP.Net is munging the checkbox ID/Names of the checkboxes: I give
it a name like "myCheckbox" and asp.net is creating the checkboxes
with the name myCheckbox_1,myCheckBox_2, etc...
2) I've tried iterating over the submitted form to get the field
values, but to no avail.

What's the deal? *sigh* All I want is a list of
dynamically-generated checkboxes, each with the same name (ala old
HTML style checkboxes which render a comma-delimited list of values.
Help!! :)

Jack

Nov 19 '05 #2
Hi Jack,

Try this:

DynamicCheckBoxes.aspx
----------------
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<br>
<asp:Literal id="mySelections" Runat="server"></asp:Literal>
<asp:PlaceHolder ID="checkboxContainer" Runat="server" />
<br>
<asp:Button ID="run" Text="Submit" Runat="server" />
</form>
</body>

DynamicCheckBoxes.aspx.vb
-----------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

Dim userSelectionList As New CheckBoxList
userSelectionList.ID = "mylist"
userSelectionList.Items.Add(New ListItem("Value #1", "1"))
userSelectionList.Items.Add(New ListItem("Value #2", "2"))
userSelectionList.Items.Add(New ListItem("Value #3", "3"))

Dim selectedItem As New ListItem("Value #4 Selected", "4")
selectedItem.Selected = True
userSelectionList.Items.Add(selectedItem)

checkboxContainer.Controls.Add(userSelectionList)

End Sub

Private Sub run_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles run.Click
Dim userSelectionList As CheckBoxList = CType(FindControl("mylist"),
CheckBoxList)

Dim x As Integer
For x = 0 To userSelectionList.Items.Count - 1
If userSelectionList.Items(x).Selected = True Then
mySelections.Text += userSelectionList.Items(x).Value
End If

Next

End Sub
Hope that helps,
Shane

"Jack Black" <ja********@hotmail.com> wrote in message
news:il********************************@4ax.com...
Using VS.Net 2k3 to build ASP.Net app with VB code-behind pages...

Hi, all! I've been struggling with getting a dynamically-generated
CheckBoxList generated. I've finally been able to get the list
generated, but now there are two problems I haven't been able to
overcome:

1) ASP.Net is munging the checkbox ID/Names of the checkboxes: I give
it a name like "myCheckbox" and asp.net is creating the checkboxes
with the name myCheckbox_1,myCheckBox_2, etc...
2) I've tried iterating over the submitted form to get the field
values, but to no avail.

What's the deal? *sigh* All I want is a list of
dynamically-generated checkboxes, each with the same name (ala old
HTML style checkboxes which render a comma-delimited list of values.
Help!! :)

Jack

Nov 19 '05 #3
Thanks for the response. I modeled my app. after this one below which can be
found in the mdsn newsgroups:

The one thing I’m doing differently is binding the:
checkboxContainer.Controls.Add(userSelectionList)
to a dynamically created tablecell for my form:

td1.Controls.Add(checkboxcontainer)

tr1.Controls.Add(td1)

Otherwise everything else is the same. How can I find that returned control
in the form

Postback? Derivatives of this: userSelectionList =
CType(form1.Page.FindControl("mylist"), CheckBoxList) aren’t working.

I’m calling my dynamic tablerow, tablecell form build in the page_load
directive rather than

Page_init. Could this be another case of having to build each control twice?

Try this:

DynamicCheckBoxes.aspx
----------------
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<br>
<asp:Literal id="mySelections" Runat="server"></asp:Literal>
<asp:PlaceHolder ID="checkboxContainer" Runat="server" />
<br>
<asp:Button ID="run" Text="Submit" Runat="server" />
</form>
</body>

DynamicCheckBoxes.aspx.vb
-----------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

Dim userSelectionList As New CheckBoxList
userSelectionList.ID = "mylist"
userSelectionList.Items.Add(New ListItem("Value #1", "1"))
userSelectionList.Items.Add(New ListItem("Value #2", "2"))
userSelectionList.Items.Add(New ListItem("Value #3", "3"))

Dim selectedItem As New ListItem("Value #4 Selected", "4")
selectedItem.Selected = True
userSelectionList.Items.Add(selectedItem)

checkboxContainer.Controls.Add(userSelectionList)

End Sub

Private Sub run_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles run.Click
Dim userSelectionList As CheckBoxList = CType(FindControl("mylist"),
CheckBoxList)

Dim x As Integer
For x = 0 To userSelectionList.Items.Count - 1
If userSelectionList.Items(x).Selected = True Then
mySelections.Text += userSelectionList.Items(x).Value
End If

Next

End Sub
"Shane Bauer" wrote:
Hi Jack,

Try this:

DynamicCheckBoxes.aspx
----------------
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<br>
<asp:Literal id="mySelections" Runat="server"></asp:Literal>
<asp:PlaceHolder ID="checkboxContainer" Runat="server" />
<br>
<asp:Button ID="run" Text="Submit" Runat="server" />
</form>
</body>

DynamicCheckBoxes.aspx.vb
-----------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

Dim userSelectionList As New CheckBoxList
userSelectionList.ID = "mylist"
userSelectionList.Items.Add(New ListItem("Value #1", "1"))
userSelectionList.Items.Add(New ListItem("Value #2", "2"))
userSelectionList.Items.Add(New ListItem("Value #3", "3"))

Dim selectedItem As New ListItem("Value #4 Selected", "4")
selectedItem.Selected = True
userSelectionList.Items.Add(selectedItem)

checkboxContainer.Controls.Add(userSelectionList)

End Sub

Private Sub run_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles run.Click
Dim userSelectionList As CheckBoxList = CType(FindControl("mylist"),
CheckBoxList)

Dim x As Integer
For x = 0 To userSelectionList.Items.Count - 1
If userSelectionList.Items(x).Selected = True Then
mySelections.Text += userSelectionList.Items(x).Value
End If

Next

End Sub
Hope that helps,
Shane

"Jack Black" <ja********@hotmail.com> wrote in message
news:il********************************@4ax.com...
Using VS.Net 2k3 to build ASP.Net app with VB code-behind pages...

Hi, all! I've been struggling with getting a dynamically-generated
CheckBoxList generated. I've finally been able to get the list
generated, but now there are two problems I haven't been able to
overcome:

1) ASP.Net is munging the checkbox ID/Names of the checkboxes: I give
it a name like "myCheckbox" and asp.net is creating the checkboxes
with the name myCheckbox_1,myCheckBox_2, etc...
2) I've tried iterating over the submitted form to get the field
values, but to no avail.

What's the deal? *sigh* All I want is a list of
dynamically-generated checkboxes, each with the same name (ala old
HTML style checkboxes which render a comma-delimited list of values.
Help!! :)

Jack


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dm_dal | last post by:
Is there a know issue surrounding the CheckBoxList control and it's viewstate? When my control is created, it's ListItems are checked as needed, but on a postback, they loose their Selected...
4
by: Shaul Feldman | last post by:
Hello, I have something really awkward at work - fighting with CheckBoxList... How can I define CSS for ListItem in CheckBoxList programmatically. I add CheckBoxList's Items on the fly, something...
5
by: Eirik Eldorsen | last post by:
I'm trying to code a reapter that for each listelement show a checkboxlist. I'm almost there. The only thing I can't figure out is how to set the ID of the checkboxlists. This is my code:...
4
by: Bass Pro | last post by:
Hi, I am creating textbox, radiobuttonlist and checkboxlist dynamically depending on data from a table. It is a questionnaire. I add the control on a Panel control during the 1st load_page event....
5
by: Patrick.O.Ige | last post by:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList is in a Datalist) By doing "li.Selected = True" i can see all the checkBoxes are selected. But what i want is to be able...
4
by: Patrick.O.Ige | last post by:
I have a CheckBoxList in a DataList and i'm trying to get item Selected after doing a postBack. I have set my CheckBoxlist AutoPostBack="True" Any ideas what 'm doing wrong? It seems not to...
2
by: Patrick.O.Ige | last post by:
I have some boolean value(1 or 0 ) in a table and i want a databinded CheckBoxList to present the selected values on the page.. With CheckBox i know i can se the Checked property like so :-...
1
by: segue | last post by:
I'm dynamically creating/populating a checkbox list and adding it to a web form. I want to when checking an item in the list have the autopostback retrieve the selected item. I'm dynamically...
1
by: Spoogledrummer | last post by:
Hi, I'm using VB.Net for this. My problem is that I've created a page that queries a table to populate a CheckBoxList of locations for the user to select from to be displayed elsewhere in the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.