ASPxCheckBoxs disappear in ASPxFormLayout after a PostBack

Alessandro 1 Reputation point
2022-11-28T15:02:05.05+00:00

I have an ASPxFormLayout which cointains a ASPxComboBox, like this:

<dx:ASPxFormLayout runat="server" ID="MyFormLayout" Width="100%">  
        <Items>  
            <dx:LayoutGroup Width="100%" Caption="Dati Anagrafica" ColumnCount="3">  
                <Items>  
                    <dx:LayoutItem>  
                        <LayoutItemNestedControlCollection>  
                            <dx:LayoutItemNestedControlContainer>  
                              
                                <dx:ASPxComboBox ID="MyCombobox" runat="server" AutoPostBack="true" OnValueChanged="MyCombobox_ValueChanged"/>  
                                  
                            </dx:LayoutItemNestedControlContainer>  
                        </LayoutItemNestedControlCollection>  
                    </dx:LayoutItem>  
                </Items>  
            </LayoutGroup>  
        </Items>  
</ASPxFormLayout>  

I also need to add some CheckBoxes which I cannot add ASPx-side, cause their number is dynamic:

protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!IsPostBack)  
            {  
                LayoutGroup layoutGroup = new LayoutGroup  
                {  
                    Caption = "Services",  
                    ColumnCount = 4  
                };  
                List<Services> services = ...  
                foreach (Services service in services)  
                {  
                    LayoutItem layoutItem = new LayoutItem  
                    {  
                        Caption = service.ServiceName;  
                    };  
                    ASPxCheckBox checkBox = new ASPxCheckBox  
                    {  
                        Value = service.ID;  
                    };  
                    layoutItem.Controls.Add(checkBox);  
                    layoutGroup.Items.Add(layoutItem);  
                }  
                MyFormLayout.Items.Add(layoutGroup);  
            }  
        }  

And I obtain the result I want, But when the value of MyCombobox is changed, a PostBack is performed, but the CheckBoxes disappear.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,405 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 27,581 Reputation points
    2022-11-28T15:24:37.797+00:00

    And I obtain the result I want, But when the value of MyCombobox is changed, a PostBack is performed, but the CheckBoxes disappear.

    This is the expected results since the checkbox is not programmatically added on every post back. Programmatically added controls must be added on every post back.

    The recommended approach is adding server control to a standard data control or the markup.


  2. Alessandro 1 Reputation point
    2022-11-28T17:00:29.713+00:00

    @AgaveJoe I am afraid that adding them on every postback will make them "forget" what Checkbox user has selected :/