0

I am loading a set of CheckBox controls into a Placeholder which is part of an UpdatePanel as below:

<asp:UpdatePanel ID="panelAddProducts" runat="server" >
 <ContentTemplate>
  <asp:PlaceHolder ID="phCategoryProducts" runat="server" />
 </ContentTemplate>
</asp:UpdatePanel>

Here's the loop to add controls:

foreach (DataRow row in Products.Rows)
{
 CheckBox chkBoxToAdd = new CheckBox();
 chkBoxToAdd.ID = "chkAddProduct_" + row["product_id"].ToString();
 chkBoxToAdd.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
 chkBoxToAdd.AutoPostBack = true;
 phCategoryProducts.Controls.Add(chkBoxToAdd);
}

I need to loop through the checked controls like below, but because the CheckedChanged causes a full postback, the controls are cleared before the event fires. How can I stop the controls being cleared on full post back?

Adam92
  • 436
  • 8
  • 23
  • If your loop to add the controls is running on every page load and re-adding them, that's what is clearing them out. If that's the case you'll need to run that loop only on the first page load, perhaps by checking if (!IsPostback) – erastl Jun 10 '21 at 14:19
  • @erastl the controls are added from a Button click event further up the page which is also in an UpdatePanel. – Adam92 Jun 10 '21 at 14:27
  • Try adding them on Page_Init. – wazz Jun 10 '21 at 23:11

0 Answers0