jeudi 10 juin 2021

ASP.NET C# CheckBox CheckedChanged Event Causing Full Postback & Clearing Controls

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?




Aucun commentaire:

Enregistrer un commentaire