2

我正在使用 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事件与它们相关联。然后我将这些放在更新面板中。我也尝试创建两个AsyncPostBackTriggers 但这也不起作用。你能告诉我上面提供的代码片段有什么问题吗?

4

1 回答 1

2
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);
于 2012-05-05T14:20:25.907 回答