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