动态单选按钮选中已更改事件不会触发

本文关键字:事件 单选按钮 动态 | 更新日期: 2023-09-27 18:33:20

我正在使用 C# 动态创建两个单选按钮,但 CheckedChange 事件没有触发。我正在使用以下代码:

RadioButton enable = new RadioButton();
enable.ID = "enable";
enable.Text = "Enable";
enable.GroupName = "enableDisableGroup";
enable.CheckedChanged += new EventHandler(enableRadioButton_CheckedChanged);
RadioButton disable = new RadioButton();
disable.ID = "disable";
disable.Text = "Disable";
disable.GroupName = "enableDisableGroup";
disable.CheckedChanged += new EventHandler(disableRadioButton_CheckedChanged);
UpdatePanel upEnableDisable = new UpdatePanel();
upEnableDisable.UpdateMode = UpdatePanelUpdateMode.Conditional;
upEnableDisable.ContentTemplateContainer.Controls.Add(enable);
upEnableDisable.ContentTemplateContainer.Controls.Add(disable);
AsyncPostBackTrigger enableTrigger = new AsyncPostBackTrigger();
enableTrigger.ControlID = enable.ID;
enableTrigger.EventName = "CheckedChanged";
upEnableDisable.Triggers.Add(enableTrigger);
AsyncPostBackTrigger disableTrigger = new AsyncPostBackTrigger();
disableTrigger.ControlID = disable.ID;
disableTrigger.EventName = "CheckedChanged";
upEnableDisable.Triggers.Add(disableTrigger);
mainDiv.Controls.Add(upEnableDisable);

创建单选按钮后,我将Checked Changed事件与它们相关联。然后我把它们放在一个更新面板中。我还尝试创建两个AsyncPostBackTrigger,但也没有用。你能告诉我上面提供的代码片段有什么问题吗?

动态单选按钮选中已更改事件不会触发

RadioButton enable = new RadioButton();
enable.ID = "enable";
enable.Text = "Enable";
enable.AutoPostBack = true;
enable.GroupName = "enableDisableGroup";
enable.CheckedChanged += new EventHandler(enableRadioButton_CheckedChanged);
RadioButton disable = new RadioButton();
disable.ID = "disable";
disable.Text = "Disable";
disable.AutoPostBack = true;
disable.GroupName = "enableDisableGroup";
disable.CheckedChanged += new EventHandler(disableRadioButton_CheckedChanged);
UpdatePanel upEnableDisable = new UpdatePanel();
upEnableDisable.UpdateMode = UpdatePanelUpdateMode.Conditional;
upEnableDisable.ContentTemplateContainer.Controls.Add(enable);
upEnableDisable.ContentTemplateContainer.Controls.Add(disable);
AsyncPostBackTrigger enableTrigger = new AsyncPostBackTrigger();
enableTrigger.ControlID = enable.ID;
enableTrigger.EventName = "CheckedChanged";
upEnableDisable.Triggers.Add(enableTrigger);
AsyncPostBackTrigger disableTrigger = new AsyncPostBackTrigger();
disableTrigger.ControlID = disable.ID;
disableTrigger.EventName = "CheckedChanged";
upEnableDisable.Triggers.Add(disableTrigger);
mainDiv.Controls.Add(upEnableDisable);