|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Dynamic CheckBoxList???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 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 Show quoteHide quote "Jack Black" <jackisb***@hotmail.com> wrote in message news:ilmt51dugi4tr525o8rqok8cgaam3a0mfk@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 > > > 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 Show quoteHide quote "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" <jackisb***@hotmail.com> wrote in message > news:ilmt51dugi4tr525o8rqok8cgaam3a0mfk@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 > > > > > > > > >
Other interesting topics
Session("passed") = Session("passed") + 1 error
cheap and reliable ASP.Net web hoster FormsAuthentication.SignOut() not working when manually creating a ticket? Server.GetLastError Hide / Show Details Size vs Width in ASP.net Q: select value in dropdown list DataReader... and ... Sending emails Q: sum grid column |
|||||||||||||||||||||||