我想另一个添加eventHandler到RadioButton.这是示例代码(正在运行):
ASP.NET:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" Text="Button" />
Run Code Online (Sandbox Code Playgroud)
C#:
protected void Page_Load(object sender, EventArgs e)
{
RadioButton RB1 = new RadioButton();
RB1.ID = "1";
RB1.GroupName = "bla";
RB1.CheckedChanged += new EventHandler(CheckedChanged);
RadioButton RB2 = new RadioButton();
RB2.ID = "2";
RB2.GroupName = "bla";
RB2.CheckedChanged += new EventHandler(CheckedChanged);
PlaceHolder1.Controls.Add(RB1);
PlaceHolder1.Controls.Add(RB2);
}
protected void CheckedChanged(object sender, EventArgs e)
{
Label1.Text = ((RadioButton)sender).ID;
}
Run Code Online (Sandbox Code Playgroud)
在我的项目中,我有动态创建RadioButtons(从数据库中获取的行数).相同的添加eventHandler不起作用,但如果我写
MyRadioButton.Load += new EventHandler(Another_method);
Run Code Online (Sandbox Code Playgroud)
在 …