locked
Problem editing checkbox ListViewItems by getting new values RRS feed

  • Question

  • User-1072848465 posted

    Is it possible to iteriate through ListViewDataItems to save changes to checkboxes in a ListView? My problem is when I modify the checkboxes the new values are not being passed to my code behind.

    Here is my ListView ItemTemplate is is being populated by binding a list in code behind

    <ItemTemplate>
    <asp:CheckBox ID="standardChkBox" runat="server" Checked='<%# Convert.ToBoolean(Eval("Applied")) %>'Text='<%# Eval("Abbreviation") %>' />
    </ItemTemplate>

    Once the list is displayed on the page I need to have the ability for the user to modify the checkboxes and save them back to the table. I have an OnClick event on a button that calls this

        protected void applyStandardsBtn_Click(object sender, EventArgs e)
        {
            
            foreach (ListViewDataItem item in StandardsListView.Items)
            {
                        bool chkBox = false;
                        CheckBox checkselect = item.FindControl("standardChkBox") as CheckBox;
                        if (checkselect.Checked)
                            chkBox = true;
                        Byte bchkBox = Convert.ToByte(chkBox);
    
                        Label lblStandardId = item.FindControl("standID_lbl") as Label;
                        int iStandardID = Convert.ToInt32(lblStandardId.Text);
    
                        using (var context = new CurriculumModelContainer())
                        {
                            AppliedStandard s = context.AppliedStandards
                                    .First(i => i.Id == iStandardID);
                            s.applied = bchkBox;
                            context.SaveChanges();
    
                        }
            }
        }

    How can I retrieve the new values on the checkboxes?

    Thanks!


    Wednesday, August 14, 2013 7:46 AM

Answers

  • User697462465 posted

    Hi bdassow,

    I have tested your code, and it can work well, so I think your problem may be caused not place the ListView bind code to the !IsPostBack. please try to place the Bind code in if(@IsPostBack){}.

    If you have any question please let me know.

    Best Regards,
    Terry Guo

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 15, 2013 7:14 AM