Search code examples
c#asp.netrepeater

Getting repeater item checkbox values when calling command from footer


Have following repeater control with a list of checkboxes:

<asp:Repeater ID="rptItemList" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
    <div>
        <asp:CheckBox ID="chkItem" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ItemName").ToString() %>' />
        <asp:HiddenField ID="hdItem" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "ItemId").ToString() %>' />
    </div>
</ItemTemplate>
<FooterTemplate>
    <asp:LinkButton ID="lbtnDel" runat="server" Text="Delete" OnClick="lbtnDel_Click" OnClientClick="return confirm('Are you sure you want to delete selected items from this list?')"></asp:LinkButton>
</FooterTemplate>
</asp:Repeater>

and following back code to handle the lbtnDel_Click event:

    protected void lbtnDel_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem ri in rptItemList.Items)
        {
            CheckBox chk = (CheckBox)ri.FindControl("chkItem");
            HiddenField hd = (HiddenField)ri.FindControl("hdItem");

            if (chk.Checked)
            {
                var tc = new ItemController();
                tc.DeleteItem(Convert.ToInt32(hd.Value));
            }
        }
        Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
    }

When I select a checkbox and click delete, the code finds the checkbox but reads it as unchecked so doesn't delete the item.

Any ideas?


Solution

  • Not 100% sure, but are you doing data binding in every page load? Try binding only on !IsPostBack Whenever I have any issues like this, it's usually because the Page Load has caused the repeater to re-bind and killed all the current state