CheckedChanged event for flag does not fire for dynamic flags

It seems I have a problem handling the event for a group of dynamic flags. The code is posted here. I thought it would be pretty straightforward, the flags would not appear in the repeater, datagrid, etc. They appear in the table located inside the div, which is located in the center of the screen. Any help would be greatly appreciated.

            foreach (SelectAssignedRolesByUserResult role in assignedRoles)
            {
                CheckBox cb = new CheckBox();
                cb.ID = string.Format("CheckBox_{0}_{1}", role.role_nm, role.role_id);
                cb.Text = role.role_nm;
                cb.Attributes.Add("role_id", role.role_id.ToString());
                cb.Attributes.Add("assigned_role_id", role.assigned_role_id.ToString());
                cb.Checked = (role.assigned_role_id > 0);
                cb.CheckedChanged += new EventHandler(cb_CheckedChanged);

                TableCell cell = new TableCell();
                TableRow row = new TableRow();

                cell.Controls.Add(cb);
                row.Cells.Add(cell);
                TableAssignedRoles.Rows.Add(row);
            }
+3
source share
2 answers

You do not specify where code is called that dynamically adds flags. I assume that you put this in the Page_Load event handler or in a sub called internally by Page_Load.

If so, move it from Page_Load to Page_Init.

:

, , Viewstate. _ , viewstate, Page_Load.

cb.AutoPostBack = true;
+1

- ? ?

, ViewState, .

0

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


All Articles