ASP.NET controls are not dynamically displayed from code

I am adding a dynamically constructed set of checkboxes to an asp.net page from code located with something like this in a recursive way:

pnlPageAccessList.Controls.Add(myCheckboxControl);

The controls are displayed on the page, but they are not displayed when I look at the source, and also can not access them from the code. If I add controls to the on_init method, they will work. But I have some business rules that make changes to the list of controls themselves, which require me to run the add method elsewhere. Has anyone seen this before? I do not work, so I can not copy the exact code.

I have two terrible ideas on how to make it work. One includes some jQuery and a set of hidden controls containing a large array of integers; another runs the on_init AND method on my other events, so the controls at least show up. Both smell like ugly khaki. The second, which, I suspect, will not work to read the values ​​from the flags.

+3
source share
4 answers

On the server side, the page is recreated from scratch every postback, so if you add any controls dynamically, you must re-add them every time you postback.

, , "" . , , , Controls, .

+4

ID , FindControl , .

+3

@Anero , FindControl.

. .

, , , . , , Init, , , ( ) . PreRender .

+1

, , . . On_Init, - .

If anyone is interested, here is jQuery for finding all checked checkboxes and putting their value attribute in a hidden control in a comma-separated list:

<script type="text/javascript">
    $(document).ready(function () {
        $('[id*=PagesPanel]').find(':checkbox').click(function () {
            $('[id*=PagesPanel]').find(':checked').each(function () {
                $('[id*=lblHiddenPageArray]').append($(this).val() + ", ");
            });
        });
    });
</script>
0
source

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