By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,665 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,665 IT Pros & Developers. It's quick & easy.

Dynamic CheckBoxList???

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.