dynamic checkbox list challenge galore

G

Guest

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:
 
R

Ryan Ternier

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
 
G

Guest

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
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top