Dynamic CheckBoxList???

Discussion in 'ASP .Net' started by Jack Black, Apr 14, 2005.

  1. Jack Black

    Jack Black Guest

    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
     
    Jack Black, Apr 14, 2005
    #1
    1. Advertising

  2. Jack Black

    Shane Bauer Guest

    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" <> wrote in message
    news:...
    > 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
    >
    >
    >
     
    Shane Bauer, Apr 14, 2005
    #2
    1. Advertising

  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" <> wrote in message
    > news:...
    > > 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
    > >
    > >
    > >

    >
    >
    >
     
    =?Utf-8?B?c2VndWU=?=, Jun 22, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Francois Verbeeck
    Replies:
    0
    Views:
    1,896
    Francois Verbeeck
    Jun 24, 2003
  2. Khan Imran

    Re: checkboxlist control

    Khan Imran, Jun 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    469
    Khan Imran
    Jun 30, 2003
  3. Ignacio Martinez

    Re: checkboxlist control

    Ignacio Martinez, Jun 30, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    455
    Ignacio Martinez
    Jun 30, 2003
  4. JD
    Replies:
    5
    Views:
    11,728
    NetArchitect
    Aug 8, 2007
  5. Patrick.O.Ige
    Replies:
    5
    Views:
    19,187
    Patrick.O.Ige
    Jun 19, 2005
Loading...

Share This Page