我正在动态创建一个asp.net RadioButton
并将其插入我的网页:
RadioButton rb = new RadioButton();
rb.ID = "rb" + ReportPKey;
rb.Text = "ReportName";
phUiControls.Controls.Add(rb);
我有以下方法:
protected void rb_CheckedChanged( object sender, EventArgs e )
{
// stuff
}
如何点击此按钮,以便在点击rb_CheckedChanged
时触发rb
?
答案 0 :(得分:1)
使用匿名委托执行此操作的一种方法(对于这些类型的方案是干净的):
var rb = new RadioButton{ ID = "rb" + ReportPKey, Text = "ReportName" };
phUiControls.Controls.Add( rb );
rb.CheckedChanged += (sender, e) => {
// event handler code
};
或(正如其他几位人士指出的那样):
rb.CheckedChanged += rb_CheckedChanged
在页面生命周期的上下文中:
protected override OnInit( EventArgs e ){
BuildDynamicControls();
base.OnInit( e );
}
private void BuildDynamicControls(){
var rb = new RadioButton{ ID = "rb" + ReportPKey, Text = "ReportName" };
phUiControls.Controls.Add( rb );
rb.CheckedChanged += (sender, e) => {
// event handler code
};
}
答案 1 :(得分:1)
rb.CheckedChanged += rb_CheckedChanged
答案 2 :(得分:0)
RadioButton rb = new RadioButton();
rb.ID = "rb"+ReportPKey;
rb.Text = ReportName ;
rb.CheckedChanged += rb_CheckedChanged
phUiControls.Controls.Add(rb);