赞
踩
单选按钮:
GroupName:如果一组按钮为互斥关系,他们的GroupName属性可设置为相同值;
OnCheckedChanged:
举例:
单选按钮往往是同时出现;
当点击 选中什么动物? 按钮,右侧文本框显示的是被选中单选按钮的Text属性;
下面例程是当选中单选按钮后,点击button按钮,在文本框中读取单选按钮值:
asp.net源代码:
<div>
<asp:RadioButton ID="rbtn_1" runat="server" Text="猫" GroupName="animal"/>
<asp:RadioButton ID="rbtn_2" runat="server" Text="狗" GroupName="animal"/>
<asp:RadioButton ID="rbtn_3" runat="server" Text="猪" GroupName="animal"/>
<asp:RadioButton ID="rbtn_4" runat="server" Text="羊" GroupName="animal"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>
<asp:TextBox ID="txt_result" runat="server"></asp:TextBox>
</div>
控件代码:
protected void Button1_Click(object sender, EventArgs e)
{
if (rbtn_1.Checked == true) { txt_result.Text = "选中了" + rbtn_1.Text; }
if (rbtn_2.Checked == true) { txt_result.Text = "选中了" + rbtn_2.Text; }
if (rbtn_3.Checked == true) { txt_result.Text = "选中了" + rbtn_3.Text; }
if (rbtn_4.Checked == true) { txt_result.Text = "选中了" + rbtn_4.Text; }
}
网页效果:
下面例程是,当在文本框中输入数据,点击button按钮,则选中单选按钮:
asp.net源代码:
<div>
<asp:RadioButton ID="rbtn_1" runat="server" Text="不及格" GroupName="animal"/>
<asp:RadioButton ID="rbtn_2" runat="server" Text="及格" GroupName="animal"/>
<asp:RadioButton ID="rbtn_3" runat="server" Text="良好" GroupName="animal"/>
<asp:RadioButton ID="rbtn_4" runat="server" Text="优秀" GroupName="animal"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:TextBox ID="txt_num" runat="server"></asp:TextBox>
</div>
控件代码:
protected void Button1_Click(object sender, EventArgs e)
{
double d_num = double.Parse(txt_num.Text);
if (d_num >= 0 && d_num < 60) { rbtn_1.Checked = true; }
if (d_num >= 60 && d_num < 80) { rbtn_2.Checked = true; }
if (d_num >= 80 && d_num < 100) { rbtn_3.Checked = true; }
if (d_num >= 100 && d_num <= 120) { rbtn_4.Checked = true; }
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。