TableRows dynamically added not in row assembly in Postback

I added TableRows through the code for the table. Lines are displayed on the page, but on PostBack, the lines are no longer part of the collection. What is the trick to keep these lines saved so that I can use them in PostBack?

.ascx

<asp:Table id="OrderItemsTable" runat="server"> 

.ascx.cs

 TableRow itemRow = new TableRow(); // Ticket Quantity TableCell cell1 = new TableCell(); cell1.Attributes.Add("align", "center"); TextBox ticket1QuantityTextBox = new TextBox(); ticket1QuantityTextBox.Width = Unit.Pixel(30); ticket1QuantityTextBox.MaxLength = 3; ticket1QuantityTextBox.Attributes.Add("class", "OrderItemTicketQuantityTB"); ticket1QuantityTextBox.Text = item.Quantity.ToString(); cell1.Controls.Add(ticket1QuantityTextBox); itemRow.Cells.Add(cell1); ... OrderItemsTable.Rows.Add(itemRow); 
+4
source share
2 answers

Yes, you need to build a control tree with your dynamic controls in the Page_Int event so that ASP.NET can load ViewState data later in the page control tree (you will need to access the values ​​from the TextBox)

On the side, dynamic control should not be used only when the type of control is dynamic. In addition, you can achieve the same result using Repeater and listview

A true understanding of dynamic controls

And here's an alternative to using dynamic controls

ASP.NET: Alternatives to Dynamic Controls - Part 1

+1
source

You need to configure dynamic controls in the Page_Init event. Perhaps you are doing this in Page_Load or elsewhere. If they are added later than Page_Init , then they do not participate in the ViewState, which will cause problems.

+3
source

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


All Articles