<asp:radiobutton id= "RadioButton1 " runat= "server " Text= "博士 " GroupName= "know "> </asp:radiobutton> <asp:radiobutton id= "RadioButton2 " runat= "server " Text= "硕士 " GroupName= "know "> </asp:radiobutton> <asp:radiobutton id= "RadioButton3 " runat= "server " Text= "本科 " GroupName= "know "> </asp:radiobutton> <asp:radiobutton id= "RadioButton4 " runat= "server " Text= "大专 " GroupName= "know "> </asp:radiobutton> <asp:radiobutton id= "RadioButton5 " runat= "server " Text= "中专 " GroupName= "know "> </asp:radiobutton> </td>
怎样在.cs 文件中获取 已经CHECKED的radiobutton的值
为什么不使用“RadioButtonList”,RadioButtonList1.SelectedItem.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (Control item in form1.Controls)
{
try
{
RadioButton rb = ((RadioButton)item);
if (rb.Checked)
{
Response.Write(rb.Text + "被选中!");
}
}
catch { }
}
}
}
循环父容器控件中的控件,看哪个已经被选中,获取其值
使用RadioButtonList,这个连分组都省了,直接用RadioButtonList.SelectedItem.Text取文本值,用RadioButtonList.SelectedValue取Value的值. 你上面那种写法,必须用for循环每一个RadioButton,还要判断哪个被选中,在取相应的文本。
页面端:
<Panel id= "Panel1" runat= "server ">
<asp:radiobutton id= "RadioButton1 " runat= "server " Text= "博士 " GroupName= "know "> </asp:radiobutton> <asp:radiobutton id= "RadioButton2 " runat= "server " Text= "硕士 " GroupName= "know "> </asp:radiobutton> <asp:radiobutton id= "RadioButton3 " runat= "server " Text= "本科 " GroupName= "know "> </asp:radiobutton> <asp:radiobutton id= "RadioButton4 " runat= "server " Text= "大专 " GroupName= "know "> </asp:radiobutton> <asp:radiobutton id= "RadioButton5 " runat= "server " Text= "中专 " GroupName= "know "> </asp:radiobutton> </Panel>
服务端:
private string GetSelectNoteValue()
{
Panel Panel1 = new Panel();
string strText = string.Empty;
for (int i = 1; i <= 5; i++)
{
RadioButton rbtn = (RadioButton)Panel1.FindControl("radioButton" + i.ToString());
if (rbtn.Checked)
{
strText = rbtn.Text;
break;
}
}
return strText;
}
建议你用RadioButtonList.