dynamic checkbox list challenge galore

Discussion in 'ASP .Net' started by =?Utf-8?B?c2VndWU=?=, Aug 4, 2005.

  1. I'm dynamically creating/populating a checkbox list and adding it to a web
    form.
    I want to when checking an item in the list have the autopostback retrieve
    the
    selected item.

    I'm dynamically adding that control as:
    Protected WithEvents userSelectionList, Ulist2 As
    System.Web.UI.WebControls.CheckBoxList
    Protected WithEvents checkboxcontainer As PlaceHolder

    For i = 0
    Dim selectedItem As New ListItem(appstring, appstring)
    'selectedItem.Selected = True
    userselectionlist.Items.Add(selectedItem)
    Next i

    checkboxcontainer.Controls.Add(userselectionlist)

    etc..
    td1.Controls.Add(checkboxcontainer)
    tr1.Controls.Add(td1)
    table1.Controls.Add(tr1)
    form1.Controls.Add(table1)

    My asp looks something like this:

    <form id="Form1" method="post" runat="server">
    <asp:Table Runat="server" ID="Table1">
    <asp:TableRow ID='appinfo' Visible=false Width='250'>
    <asp:TableCell ID='appname' Visible=true ColumnSpan="1"
    HorizontalAlign="Right">
    <br>
    <asp:Literal id="mySelections" Runat="server"></asp:Literal>
    <asp:placeHolder ID="checkboxcontainer" Runat="server" />
    <asp:CheckBoxList ID="mylist" Runat="server" />
    <br>
    </asp:TableCell>
    </asp:TableRow>

    And retrieving the list on the postback which I'm calling in page_init:

    Sub apppb()
    Dim i, x As Integer

    'Dim userSelectionList As New CheckBoxList
    userSelectionList = CType(form1.FindControl("mylist"), CheckBoxList)
    Ulist2 = CType(form1.FindControl("mylist"), CheckBoxList)
    Try
    For x = 0 To Ulist2.Items.Count - 1
    If Ulist2.Items(x).Selected = True Then
    results.Text += Ulist2.Items(x).Value
    'mySelections.Text += userSelectionList.Items(x).Value

    End If
    Next x
    Catch ex As Exception

    End Try
    End Sub

    Yet no value for the selected item.

    Please help. I've used dynamic controls before and I think
    maybe there may be a problem when I redeclare the control
    for viewstate on the postback i.e.:

    There are some complications in finding the checkboxlist control maybe because
    it's binded to a placeholder - checkboxcontainer.

    My page init looks something like:

    InitializeComponent()
    If Not IsPostBack Then
    buildform(sender, e)
    Else

    'userSelectionList = New CheckBoxList
    'checkboxcontainer = New PlaceHolder
    'userSelectionList = CType(form1.FindControl("mylist"),
    CheckBoxList)
    'checkboxcontainer.Controls.Add(userSelectionList)
    'checkcontainer()
    buildform(sender, e)
    apppb()

    End If

    Can someone get this scenario to work?

    Regards;

    tjt:
     
    =?Utf-8?B?c2VndWU=?=, Aug 4, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?c2VndWU=?=

    Ryan Ternier Guest

    segue,

    In the CBL's properties, set AutoPostBack = true

    As well, you will want to set up a server side event that is called
    whenever a user clicks the checkbox (the click event). THis way you can
    get the sender, and the EventArgs from the postback and figure out what
    Item called the post back.

    /RT
    segue wrote:
    > I'm dynamically creating/populating a checkbox list and adding it to a web
    > form.
    > I want to when checking an item in the list have the autopostback retrieve
    > the
    > selected item.
    >
    > I'm dynamically adding that control as:
    > Protected WithEvents userSelectionList, Ulist2 As
    > System.Web.UI.WebControls.CheckBoxList
    > Protected WithEvents checkboxcontainer As PlaceHolder
    >
    > For i = 0
    > Dim selectedItem As New ListItem(appstring, appstring)
    > 'selectedItem.Selected = True
    > userselectionlist.Items.Add(selectedItem)
    > Next i
    >
    > checkboxcontainer.Controls.Add(userselectionlist)
    >
    > etc..
    > td1.Controls.Add(checkboxcontainer)
    > tr1.Controls.Add(td1)
    > table1.Controls.Add(tr1)
    > form1.Controls.Add(table1)
    >
    > My asp looks something like this:
    >
    > <form id="Form1" method="post" runat="server">
    > <asp:Table Runat="server" ID="Table1">
    > <asp:TableRow ID='appinfo' Visible=false Width='250'>
    > <asp:TableCell ID='appname' Visible=true ColumnSpan="1"
    > HorizontalAlign="Right">
    > <br>
    > <asp:Literal id="mySelections" Runat="server"></asp:Literal>
    > <asp:placeHolder ID="checkboxcontainer" Runat="server" />
    > <asp:CheckBoxList ID="mylist" Runat="server" />
    > <br>
    > </asp:TableCell>
    > </asp:TableRow>
    >
    > And retrieving the list on the postback which I'm calling in page_init:
    >
    > Sub apppb()
    > Dim i, x As Integer
    >
    > 'Dim userSelectionList As New CheckBoxList
    > userSelectionList = CType(form1.FindControl("mylist"), CheckBoxList)
    > Ulist2 = CType(form1.FindControl("mylist"), CheckBoxList)
    > Try
    > For x = 0 To Ulist2.Items.Count - 1
    > If Ulist2.Items(x).Selected = True Then
    > results.Text += Ulist2.Items(x).Value
    > 'mySelections.Text += userSelectionList.Items(x).Value
    >
    > End If
    > Next x
    > Catch ex As Exception
    >
    > End Try
    > End Sub
    >
    > Yet no value for the selected item.
    >
    > Please help. I've used dynamic controls before and I think
    > maybe there may be a problem when I redeclare the control
    > for viewstate on the postback i.e.:
    >
    > There are some complications in finding the checkboxlist control maybe because
    > it's binded to a placeholder - checkboxcontainer.
    >
    > My page init looks something like:
    >
    > InitializeComponent()
    > If Not IsPostBack Then
    > buildform(sender, e)
    > Else
    >
    > 'userSelectionList = New CheckBoxList
    > 'checkboxcontainer = New PlaceHolder
    > 'userSelectionList = CType(form1.FindControl("mylist"),
    > CheckBoxList)
    > 'checkboxcontainer.Controls.Add(userSelectionList)
    > 'checkcontainer()
    > buildform(sender, e)
    > apppb()
    >
    > End If
    >
    > Can someone get this scenario to work?
    >
    > Regards;
    >
    > tjt:
    >
    >
     
    Ryan Ternier, Aug 4, 2005
    #2
    1. Advertising

  3. Hi;

    I found that you need to repopulate the control before you loop
    through it on a postback to see what was selected.

    Regards;

    Segue

    "Ryan Ternier" wrote:

    > segue,
    >
    > In the CBL's properties, set AutoPostBack = true
    >
    > As well, you will want to set up a server side event that is called
    > whenever a user clicks the checkbox (the click event). THis way you can
    > get the sender, and the EventArgs from the postback and figure out what
    > Item called the post back.
    >
    > /RT
    > segue wrote:
    > > I'm dynamically creating/populating a checkbox list and adding it to a web
    > > form.
    > > I want to when checking an item in the list have the autopostback retrieve
    > > the
    > > selected item.
    > >
    > > I'm dynamically adding that control as:
    > > Protected WithEvents userSelectionList, Ulist2 As
    > > System.Web.UI.WebControls.CheckBoxList
    > > Protected WithEvents checkboxcontainer As PlaceHolder
    > >
    > > For i = 0
    > > Dim selectedItem As New ListItem(appstring, appstring)
    > > 'selectedItem.Selected = True
    > > userselectionlist.Items.Add(selectedItem)
    > > Next i
    > >
    > > checkboxcontainer.Controls.Add(userselectionlist)
    > >
    > > etc..
    > > td1.Controls.Add(checkboxcontainer)
    > > tr1.Controls.Add(td1)
    > > table1.Controls.Add(tr1)
    > > form1.Controls.Add(table1)
    > >
    > > My asp looks something like this:
    > >
    > > <form id="Form1" method="post" runat="server">
    > > <asp:Table Runat="server" ID="Table1">
    > > <asp:TableRow ID='appinfo' Visible=false Width='250'>
    > > <asp:TableCell ID='appname' Visible=true ColumnSpan="1"
    > > HorizontalAlign="Right">
    > > <br>
    > > <asp:Literal id="mySelections" Runat="server"></asp:Literal>
    > > <asp:placeHolder ID="checkboxcontainer" Runat="server" />
    > > <asp:CheckBoxList ID="mylist" Runat="server" />
    > > <br>
    > > </asp:TableCell>
    > > </asp:TableRow>
    > >
    > > And retrieving the list on the postback which I'm calling in page_init:
    > >
    > > Sub apppb()
    > > Dim i, x As Integer
    > >
    > > 'Dim userSelectionList As New CheckBoxList
    > > userSelectionList = CType(form1.FindControl("mylist"), CheckBoxList)
    > > Ulist2 = CType(form1.FindControl("mylist"), CheckBoxList)
    > > Try
    > > For x = 0 To Ulist2.Items.Count - 1
    > > If Ulist2.Items(x).Selected = True Then
    > > results.Text += Ulist2.Items(x).Value
    > > 'mySelections.Text += userSelectionList.Items(x).Value
    > >
    > > End If
    > > Next x
    > > Catch ex As Exception
    > >
    > > End Try
    > > End Sub
    > >
    > > Yet no value for the selected item.
    > >
    > > Please help. I've used dynamic controls before and I think
    > > maybe there may be a problem when I redeclare the control
    > > for viewstate on the postback i.e.:
    > >
    > > There are some complications in finding the checkboxlist control maybe because
    > > it's binded to a placeholder - checkboxcontainer.
    > >
    > > My page init looks something like:
    > >
    > > InitializeComponent()
    > > If Not IsPostBack Then
    > > buildform(sender, e)
    > > Else
    > >
    > > 'userSelectionList = New CheckBoxList
    > > 'checkboxcontainer = New PlaceHolder
    > > 'userSelectionList = CType(form1.FindControl("mylist"),
    > > CheckBoxList)
    > > 'checkboxcontainer.Controls.Add(userSelectionList)
    > > 'checkcontainer()
    > > buildform(sender, e)
    > > apppb()
    > >
    > > End If
    > >
    > > Can someone get this scenario to work?
    > >
    > > Regards;
    > >
    > > tjt:
    > >
    > >

    >
     
    =?Utf-8?B?c2VndWU=?=, Aug 5, 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. George Addison

    Server errors galore, local runs fine

    George Addison, Nov 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    318
    Tommy
    Nov 29, 2003
  2. =?Utf-8?B?cm9kY2hhcg==?=

    tabs galore

    =?Utf-8?B?cm9kY2hhcg==?=, Feb 5, 2007, in forum: ASP .Net
    Replies:
    7
    Views:
    418
    Mark Rae
    Feb 6, 2007
  3. Replies:
    18
    Views:
    524
  4. Replies:
    2
    Views:
    344
    Manish Pandit
    Oct 3, 2007
  5. kenny
    Replies:
    1
    Views:
    198
    Thomas 'PointedEars' Lahn
    Jan 3, 2011
Loading...

Share This Page