动态单选按钮CheckedChanged事件不会触发

时间:2012-05-05 13:51:57

标签: c# asp.net updatepanel dynamic-controls

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

1 个答案:

答案 0 :(得分: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);