Reputation: 1800
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?
Upvotes: 6
Views: 11460
Reputation: 1175
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;
}
Upvotes: 0
Reputation: 14921
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
Upvotes: 10