首页 新闻 赞助 找找看

Radiobutton怎么获取值

0
悬赏园豆:5 [已关闭问题]

 <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的值

中国女孩的主页 中国女孩 | 初学一级 | 园豆:175
提问于:2010-06-07 16:02
< >
分享
其他回答(2)
0

为什么不使用“RadioButtonList”,RadioButtonList1.SelectedItem.Text;

Astar | 园豆:40805 (高人七级) | 2010-06-07 16:10
0

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 { }
        }

      
    }
}

循环父容器控件中的控件,看哪个已经被选中,获取其值

老级菜鸟 | 园豆:235 (菜鸟二级) | 2010-06-07 16:24
0

使用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.

love_99 | 园豆:325 (菜鸟二级) | 2010-06-08 14:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册