6

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?

HuwD
  • 1,800
  • 4
  • 27
  • 58
  • +1 for awesomeness. I hadn't thought of using a hidden field in the repeater for storing values. If I could up-vote more, I would. – John Smith Mar 15 '14 at 00:54

2 Answers2

10

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

Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117
  • Lol, school boy error. Yeah the databind was being performed in the page load and killing the state. Thanks – HuwD Jul 04 '13 at 10:52
0

I had a repeater inside an update panel. So the only control in the RepeaterItem was a DataBoundLiteralControl

This worked for me:

foreach (RepeaterItem item in rpLists.Items)
                {

                    if (item.Controls.Count > 0)
                    {
                        DataBoundLiteralControl dbLt = item.Controls[0] as DataBoundLiteralControl;
                        if (dbLt != null)
                        {
                            var controlCollection = this.ParseControl(dbLt.Text);
                            HtmlInputCheckBox cbInclude = (HtmlInputCheckBox) FindControl(controlCollection, "cbIncludeList");
                            if (cbInclude != null)
                            {
                                if (cbInclude.Checked)
                                {
                                    //your code here 
                                }
                            }
                        }
                    }
                }

I had to create a recursive method to for FindControl, something about it not working if its not part of a page. Shrug See here ASP.Net FindControl is not working - How come?

private Control FindControl(Control parent, string id)
        {
            if (parent.ID == id)
                return parent;

            if (parent.HasControls())
            {
                foreach (Control childControl in parent.Controls)
                {

                    if (childControl.ID == id)
                        return childControl;

                    if (childControl.HasControls())
                        return FindControl(childControl, id);
                }

            }

            return null;
        }
GregoryBrad
  • 1,145
  • 13
  • 18