How to get selected values from dynamically generated CheckBoxList? (ASP.NET)
I am generating the CheckBoxList control in Run-Time. I dont know
how to get the selected check box values from dynamically generated checkBoxList.
(I know how to select the values from CheckBoxList if it is desined at runtime)
I tried to use FindControl method but FindControl method returns NULL.
Look the code to generate the CheckBoxList in Run Time.
In Code behind...
CheckBoxList ChkMachines = new CheckBoxList();
ChkMachines.ID = "ChkMachines_" + <some variable>
ChkMachines.DataSource = ReaderMech;
// MECH_ID, MECHNAME is data base fields
// ReaderMech is OleDBDataReader
// tbale Row and cell are generated dynamically
TableRow tRowMach = new TableRow();
TableCell tCellMech = new TableCell();
// then the CheckBoxList Control is added in the table cell
I put the above code in loop.
The CheckBoxLists are generated with out any problem. But I want to know
,How to access the selected checkbox values from the dynamically generated
If any one knows Help me to solve this.
Re: How to get selected values from dynamically generated CheckBoxList? (ASP.NET)
I assume the code below is contained in an "if not ispostback"...
For some odd reason you can't dynamically create controls and have them persist
to postback, there may be a way, but I don't know it.. If you take it out
of the "if not ispostback" it will persist and strangely enough maintain
"sugumar" <firstname.lastname@example.org> wrote:
>I am generating the CheckBoxList control in Run-Time. I dont know
>how to get the selected check box values from dynamically generated checkBoxList.
>(I know how to select the values from CheckBoxList if it is desined at runtime)
>I tried to use FindControl method but FindControl method returns NULL.
>Look the code to generate the CheckBoxList in Run Time.
>In Code behind...
>CheckBoxList ChkMachines = new CheckBoxList();
>ChkMachines.ID = "ChkMachines_" + <some variable>
>ChkMachines.DataSource = ReaderMech;
>// MECH_ID, MECHNAME is data base fields
>// ReaderMech is OleDBDataReader
>// tbale Row and cell are generated dynamically
>TableRow tRowMach = new TableRow();
>TableCell tCellMech = new TableCell();
>// then the CheckBoxList Control is added in the table cell
>I put the above code in loop.
>The CheckBoxLists are generated with out any problem. But I want to know
>,How to access the selected checkbox values from the dynamically generated
>If any one knows Help me to solve this.
Anyone found a solution?
I to am having this problem.
Currently I have been able to add/get response values for almost every contorl that isn't multi selected (textbox, checkbox, radio button, combobox) but not checkboxlist and listbox. When I loop through the form responses I do not get anything returned.
Can someone help?
You can loop through just about any milti-element control by getting the count and looping through the items like an array. Looks like you are using C# and I am using VB but the concept is the same.
Dim itemCount As Integer
itemCount = thecheckboxlist.Items.Count
For i = 0 To (itemCount - 1)
If thecheckboxlist.Items(i).Selected Then
You may also use the Request.Forms("formname") to pull a collection of all the controls on that form.
I would recommend reading the help for Request.Forms. You may find it helpful.
since you are adding the checkboxlist control dynamically, it will not be persisted on postbacks. If you want the values which are selected in the checkbox list, store the selected value in hiddenfields, create the checkboxlist as done before and select the values based on the values stored in the hidden field.
hope the above helps and if the problem is solved, please do reply
I know this post is a little old but the way i read values from a checkbox list is like this...
Oh and btw...yes i am reading these values from a dynamically generated checkbox list
foreach (ListItem item in ((CheckBoxList)Form.FindControl("cblActivities")).Items)
string Activities += item.Value;
To get controls to persist you must create them before page initialization.
override protected void OnInit(EventArgs e)
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
// YOUR METHOD TO GENERATE DYNAMIC CONTROLS
protected void preInit()
// GENERATE CONTROLS
Last edited by commonMan; 06-02-2006 at 12:02 PM.
how to get selected values from checkboxlist
I think this code may help you .
Here CheckBoxList is dyanamically generated and we move the selected value from checkboxlist to lisrBox.
int x = CheckBoxList1.Items.Count;
for (int i = 0; i < x; i++)
Iterate through tCellMach.Controls
I find the best way to add dynamic controls to be:
no post back - page load
on post back - page init
if any control event on the page modifies the needed dynamic controls, I remove all the dynamic controls (tCellMach.Controls.Clear) on that event and re-add the controls (all in the event).
Last edited by mdb002; 02-13-2008 at 05:55 PM.
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL