怎么让一组RadioButton的选择改变,就执行相应的功能,不必每个RadioButton都加一个事件

lyboy8 2010-10-17 06:55:01
例如:
aspx代码:
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="rb" Text="1"
AutoPostBack="True" oncheckedchanged="RadioButton1_CheckedChanged" />
<br />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="rb" Text="2"
AutoPostBack="True" oncheckedchanged="RadioButton2_CheckedChanged1"/>
<br />
<asp:RadioButton ID="RadioButton3" runat="server" GroupName="rb" Text="3"
AutoPostBack="True" oncheckedchanged="RadioButton3_CheckedChanged"/>
<br />
<asp:RadioButton ID="RadioButton4" runat="server" GroupName="rb" Text="4"
AutoPostBack="True" oncheckedchanged="RadioButton4_CheckedChanged"/>
<br />
<asp:RadioButton ID="RadioButton5" runat="server" GroupName="rb" Text="5"
AutoPostBack="True" oncheckedchanged="RadioButton5_CheckedChanged"/>
<br />
<asp:Label ID="Label1" runat="server" Text="rb"></asp:Label>
cs代码:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
this.Label1.Text = "你选择了1.";
}
protected void RadioButton2_CheckedChanged1(object sender, EventArgs e)
{
this.Label1.Text = "你选择了2.";
}
protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
{
this.Label1.Text = "你选择了3.";
}
protected void RadioButton4_CheckedChanged(object sender, EventArgs e)
{
this.Label1.Text = "你选择了4.";
}
protected void RadioButton5_CheckedChanged(object sender, EventArgs e)
{
this.Label1.Text = "你选择了5.";
}


我想实现的是:这一组RadioButton就对应一个事件,不必要在cs文件里一个RadioButton就一个checkedchange.

现在代码少点还好,有上百个RadioButton,就很麻烦啦.

有什么方法实现呢.

希望高手指点一下,谢谢!!
...全文
444 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
大飞飞虫 2010-10-17
  • 打赏
  • 举报
回复
RadioButtonList,就是一组单选框,正解
wwfgu00ing 2010-10-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 koukoujiayi 的回复:]
用RadioButtonList,通过索引号来判断
[/Quote]

+1
begintransaction 2010-10-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
绑定同一个
protected void RadioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb=sender as RadioButton;
}
RadioButtonlist
[/Quote]
做人个共公事件出来,多次调用
lyboy8 2010-10-17
  • 打赏
  • 举报
回复
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked)
{
Label4.Text = "1";
}
else if (RadioButton2.Checked)
{
Label4.Text = "2";
}
else if (RadioButton3.Checked)
{
Label4.Text = "3";
}
else if (RadioButton4.Checked)
{
Label4.Text = "4";
}
else if (RadioButton5.Checked)
{
Label4.Text = "5";
}
}
谢谢指点,我的实现是这样,不知道还可不可以改进呢...
lyboy8 2010-10-17
  • 打赏
  • 举报
回复
我不单是一组RadioButton而已,,我有好几组,每一组控制一个Label...
lyboy8 2010-10-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
绑定同一个
protected void RadioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb=sender as RadioButton;
}
RadioButtonlist
[/Quote]
用list版面难看..
具体说一下怎么绑定到同一个..谢谢..
最后一只恐龙 2010-10-17
  • 打赏
  • 举报
回复
楼上说了,没机会了
koukoujiayi 2010-10-17
  • 打赏
  • 举报
回复
用RadioButtonList,通过索引号来判断
wuyq11 2010-10-17
  • 打赏
  • 举报
回复
绑定同一个
protected void RadioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb=sender as RadioButton;
}
RadioButtonlist

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧