Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Dynamic CheckBoxList???

Reply
Thread Tools

Dynamic CheckBoxList???

 
 
Jack Black
Guest
Posts: n/a
 
      04-14-2005
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



 
Reply With Quote
 
 
 
 
Shane Bauer
Guest
Posts: n/a
 
      04-14-2005
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>
<asplaceHolder 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
 
 
 
=?Utf-8?B?c2VndWU=?=
Guest
Posts: n/a
 
      06-22-2005
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>
<asplaceHolder 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>
> <asplaceHolder 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >
> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Variables? OR Dynamic Controls =?Utf-8?B?VGVyb3M=?= ASP .Net 1 08-10-2004 01:13 PM
Dynamic control on aspx page, dynamic location Chris Thunell ASP .Net 3 07-21-2004 04:52 PM
VPN between 2 Cisco routers (1 static, 1 dynamic) with access from stat --> dynamic over ISDN Hans-Peter Walter Cisco 3 01-21-2004 02:12 PM
Does Pix or cisco router support dynamic-to-dynamic IPSec VPN? c Cisco 2 01-13-2004 01:53 AM
Re: Dynamic Table with Dynamic LinkButtons Rick Glos ASP .Net 0 07-08-2003 01:09 PM



Advertisments