日期:2014-05-18  浏览次数:20499 次

怎样重置RadioButton
以下是我的程序中前后台的部分代码(其实RadioButton有更多,我只列出了三个)。我想在点击Button2之后实现让RadioButton1为选中状态,其余的RadioButton为未选中状态,也即初时化一下。由于所有的RadioButton都属于RG1组,为什么不能只将RadioButton1.Checked = true;就行了,还非得把其余的统统都设置为RadioButton2.Checked = false;.......,试想如果有100个RadioButton的话还得将其余99个都设置为false才行吗?不知有没有更简捷的方法?谢谢!

前台:
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="RG1" Checked="True" /><asp:Image ID="Image2" runat="server" ImageUrl="~/bbs/mood/face0.gif"/>
  <asp:RadioButton ID="RadioButton2" runat="server" GroupName="RG1" /><asp:Image ID="Image3" runat="server" ImageUrl="~/bbs/mood/face1.gif"/>
  <asp:RadioButton ID="RadioButton3" runat="server" GroupName="RG1" /><asp:Image ID="Image4" runat="server" ImageUrl="~/bbs/mood/face2.gif"/>

后台:
protected void Button2_Click(object sender, EventArgs e) //重置
  {
  RadioButton1.Checked = true;
  RadioButton2.Checked = false;
  RadioButton3.Checked = false;
  }

------解决方案--------------------
//用RadioButtonList 不就行了?
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="a">aaaaa</asp:ListItem>
<asp:ListItem Value="b">bbbbb</asp:ListItem>
<asp:ListItem Value="c">ccccc</asp:ListItem>
</asp:RadioButtonList></div> 
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Button" />


protected void Button3_Click(object sender, EventArgs e)
{
RadioButtonList1.Items.FindByValue("a").Selected = true;
}