作者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
推 tomex:if (ct is RadioButton) => better 06/20 19:10
推 ThreeDay0905:感謝 06/22 15:42