How do you save items from a CheckBoxList in a FormView?

I am using a CheckBoxList inside of a FormView with an ObjectDataSource. In order to bind the selected values to the CheckBoxList I am using the *FormView_DataBound* event to find the CheckBoxList and set the selected items. This works fine.

Now saving these values is becoming problematic. Is it possible to use the ObjectDataSource to update these values, or do I have to save them after the ObjectDataSource saves?

Asked on April 7, 2016 in ASP.NET,   C#.
Add Comment
1 Answer(s)

put this code in formview inserting event… Iterate checkbox list and add selected item in datatable and pass to your BLL

CheckBoxList chklRoles = (CheckBoxList)frm.FindControl("chklRoles");
    foreach (ListItem liRole in chklRoles.Items)
    {
        if (liRole.Selected)
        {
            SecurityDS.SC_RoleRow drwRoles = dtblRoles.NewSC_RoleRow();
            drwRoles.Name = liRole.Value;
            drwRoles.IsActive = false;
            dtblRoles.Rows.Add(drwRoles);
        }
    }
    e.Values["userRole"] = dtblRoles;

ASPX page code.. parameter type

<InsertParameters>

                    <asp:Parameter Name="userRole" Type="Object" />
                </InsertParameters>

and then iterate datatable in your BLL and save into DB accordingly

Answered on April 7, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.