How to get selected values from dynamically generated CheckBoxList? (ASP.NET)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: How to get selected values from dynamically generated CheckBoxList? (ASP.NET)

  1. #1
    sugumar Guest

    How to get selected values from dynamically generated CheckBoxList? (ASP.NET)




    Hi,
    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.DataValueField="MECH_ID";
    ChkMachines.DataTextField="MECHNAME";
    ChkMachines.RepeatColumns=2;
    ChkMachines.DataSource = ReaderMech;
    ChkMachines.DataBind();
    // MECH_ID, MECHNAME is data base fields
    // ReaderMech is OleDBDataReader

    // tbale Row and cell are generated dynamically

    TableRow tRowMach = new TableRow();
    Table2.Rows.Add(tRowMech);
    TableCell tCellMech = new TableCell();
    tRowMech.Cells.Add(tCellMech);
    // then the CheckBoxList Control is added in the table cell
    tCellMach.Controls.Add(ChkMachines);

    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
    CheckBoxList?
    If any one knows Help me to solve this.

    Thanks
    sugumar

  2. #2
    Viking Guest

    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
    it's state..

    "sugumar" <sugunsg@yahoo.com> wrote:
    >
    >
    >
    >Hi,
    >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.DataValueField="MECH_ID";
    >ChkMachines.DataTextField="MECHNAME";
    >ChkMachines.RepeatColumns=2;
    >ChkMachines.DataSource = ReaderMech;
    >ChkMachines.DataBind();
    >// MECH_ID, MECHNAME is data base fields
    >// ReaderMech is OleDBDataReader
    >
    >// tbale Row and cell are generated dynamically
    >
    >TableRow tRowMach = new TableRow();
    >Table2.Rows.Add(tRowMech);
    >TableCell tCellMech = new TableCell();
    >tRowMech.Cells.Add(tCellMech);
    >// then the CheckBoxList Control is added in the table cell
    > tCellMach.Controls.Add(ChkMachines);
    >
    >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
    >CheckBoxList?
    >If any one knows Help me to solve this.
    >
    >Thanks
    >sugumar



  3. #3
    Join Date
    Jan 2006
    Posts
    1

    Angry 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?

    -Chad

  4. #4
    Join Date
    Jan 2006
    Posts
    2
    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
    'Preform action
    End If
    Next i

  5. #5
    Join Date
    Jan 2006
    Posts
    33
    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.

  6. #6
    Join Date
    Feb 2006
    Posts
    21
    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

  7. #7
    Join Date
    Apr 2006
    Posts
    1
    I know this post is a little old but the way i read values from a checkbox list is like this...

    Code:
    foreach (ListItem item in ((CheckBoxList)Form.FindControl("cblActivities")).Items)
                {
                    if (item.Selected)
                    {
                       string Activities += item.Value;  
                        
                    }
                }
    Response.Write(Activities);
    Oh and btw...yes i am reading these values from a dynamically generated checkbox list

  8. #8
    Join Date
    Jun 2006
    Posts
    1
    To get controls to persist you must create them before page initialization.

    Example:

    override protected void OnInit(EventArgs e)
    {

    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();

    // YOUR METHOD TO GENERATE DYNAMIC CONTROLS
    preInit();

    base.OnInit(e);
    }

    protected void preInit()
    {
    // GENERATE CONTROLS
    }
    Last edited by commonMan; 06-02-2006 at 12:02 PM.

  9. #9
    Join Date
    Feb 2008
    Posts
    1

    how to get selected values from checkboxlist

    Hi Dear,
    I think this code may help you .
    Here CheckBoxList is dyanamically generated and we move the selected value from checkboxlist to lisrBox.


    ListBox1.Items.Clear();
    int x = CheckBoxList1.Items.Count;
    for (int i = 0; i < x; i++)
    {
    if (CheckBoxList1.Items[i].Selected)
    {
    ListBox1.Items.Add(CheckBoxList1.Items[i].Value);
    }
    }

  10. #10
    Join Date
    Jul 2007
    Posts
    161
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center