Loop through Check boxes and perform action

Posted on 2011-05-08
Last Modified: 2012-05-11

I have created a registration page in which I use a repeater to populate a list of check boxes showing the available controls.  The code looks like this:

  <asp:Repeater ID="UsersRoleList" runat="server">
                                <asp:CheckBox runat="server" ID="RoleCheckBox" AutoPostBack="false" Text='<%# DataBinder.Eval(Container.DataItem, "RoleName") %>'
                                <br />

DataTable roles = Check30.h30DataAccess.Roles_Get();
 UsersRoleList.DataSource = roles;

On pressing the submit button, I would like to iterate through the RoleCheckBox and if checked, add the user to the role  - i.e.  Roles.AddUserToRole(profile.UserName, RoleCheckBox.Text) or something like that.  Any help in doing this greatly appreciated.

Question by:RBS
    LVL 2

    Expert Comment

    you can manipulate the rest of the code
    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
      CheckBox c = e.Row.Cells[4].FindControl("YourCheckboxId");
      if(c != null && e.Row.DataItem["sFixed_f"] == true)
        c.Checked = true;

    Open in new window

    LVL 18

    Expert Comment

    foreach(RepeaterItem rptItem in ptProducts.Items)
     CheckBox chk = (CheckBox) rptItem.FindControl("chk_Select");
     // do stuff here;

    Author Comment


    This does not seem to work.  The name of my check box is RoleCheckBox - but name of the control on the client side would seem to be related to the value RoleName in the repeater.  I have no idea where "chk_Select" comes from.  

    Can you show how you think this would work - using the names of controls I have used.

    LVL 18

    Accepted Solution

    please read this:
    Getting Value out of a data repeater checkbox

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Normally a window is moved by clicking on the caption bar and dragging. You may want your user to be able to move borderless forms or move a form by clicking anywhere in the form without the limitation to the caption bar. There are many ways to do i…
    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video discusses moving either the default database or any database to a new volume.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now