动态生成按钮,点击未执行

我需要生成一个动态的按钮列表,我已经做了,附加了一个事件处理程序。

但是,事件处理程序未被执行。

private void GetOptions(EcoBonusRequest request) { var ecobonuswworkflow = WorkflowFactory.CreateEcobonusWorkflow(); ecobonuswworkflow.SetCurrentStep(request.CurrentStatus); var currentoptions = ecobonuswworkflow.GetCurrentOptions(); foreach(var option in currentoptions) { var btn = new Button() {Text = option.OptionName}; btn.Click +=new EventHandler(btn_Click); Buttons.Controls.Add(btn); } } void btn_Click(object sender, EventArgs e) { var btn = (Button) sender; string command = btn.Text; EcoBonusRequest request = this.GetDBRequest(RequestBaseId.Value); EcoBonusRequestBL.AddWorkflowHistoryItem(request, command,CurrentUser, command); } 

您在页面中动态添加的控件必须添加到Page_init事件中,并且它们必须具有唯一的ID。 如果要添加文本框或其他用户可以输入或更改值的控件,则在重新添加这些控件时,每个post都必须具有相同的ID。