PC Review


Reply
Thread Tools Rate Thread

dynamic checkbox list challenge galore

 
 
=?Utf-8?B?c2VndWU=?=
Guest
Posts: n/a
 
      4th Aug 2005

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:


 
Reply With Quote
 
 
 
 
Ryan Ternier
Guest
Posts: n/a
 
      4th Aug 2005
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:
>
>

 
Reply With Quote
 
=?Utf-8?B?c2VndWU=?=
Guest
Posts: n/a
 
      5th Aug 2005
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:
> >
> >

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Checkbox Raymond Microsoft ASP .NET 3 22nd Jun 2007 02:40 AM
dynamic checkbox James Wong Microsoft ASP .NET 3 13th Dec 2006 10:23 AM
HELP: Dynamic updateable checkbox list Usenet User Microsoft ASP .NET 2 10th Feb 2006 07:22 PM
Guru Challenge Request re Dynamic Scripts from Server =?Utf-8?B?TmVhbA==?= Microsoft Dot NET Framework 0 10th Mar 2005 09:53 AM
Challenge/Question for Dynamic (server created) Scripting Gurus =?Utf-8?B?TmVhbA==?= Microsoft Dot NET Framework 0 10th Mar 2005 09:51 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:15 PM.