Dynamic CheckBoxList???

J

Jack Black

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
 
S

Shane Bauer

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top