作者lostid (lostid)
看板C_Sharp
标题Re: [问题] radiobutton的简单问题
时间Fri Jun 20 14:08:50 2008
※ 引述《ThreeDay0905 (三天)》之铭言:
: 这应该是很基础的问题
: 不过翻书或着爬文都找不到
: 就是我因为版面的关系,没有用radiobuttonlist
: 只用了radiobutton,然後将其group设为一样
: 这样我在程式中要如何使用呢
: 因为每个radiobutton的id都不一样
: 我想要知道使用者选择了哪个项目
: 没办法跟radiobuttonlist一样,直接下 radiobuttonlist.selectvalue = xxxxx
: 假如在我有多个不同名称的radiobutton,都属於avg群组的话
: 我要怎麽取得现在使用者选择了哪个radiobutton
: 或是要怎麽用程式码去指定哪个radiobutton被选取呢
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//自定义属性
RadioButton1.Attributes.Add("myAttribute", "111");
RadioButton2.Attributes.Add("myAttribute", "222");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (Control ct in this.form1.Controls)
{
if (ct.GetType().ToString() == "System.Web.UI.WebControls.RadioButton")
{
//获取
RadioButton rb = ct as RadioButton;
if (rb.Checked == true)
{
Label1.Text = rb.Attributes["myAttribute"].ToString();
break;
}
}
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 222.172.196.89
1F:推 tomex:if (ct is RadioButton) => better 06/20 19:10
2F:推 ThreeDay0905:感谢 06/22 15:42