Retrieving values โ€‹โ€‹of a repeater element when invoking a command from a footer

It has the following relay control with a list of flags:

<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 the following reverse 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 the check box and click the "Delete" button, the code finds the check box, but reads it as unchecked, so it does not delete the item.

Any ideas?

+6
source share
2 answers

Not 100% sure, but do you bind data to every page load? Try to bind only to !IsPostBack Whenever I have such problems, this is usually due to the fact that loading the page caused the repeater to relink and kill all the current state

+10
source

I had a repeater inside the update panel. So the only control in RepeaterItem was 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 for FindControl, something about this does not work if it is not part of the page. shrug See Here ASP.Net FindControl not working - How did it happen?

 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; } 
0
source

Source: https://habr.com/ru/post/948700/


All Articles