Re: [問題] radiobutton的簡單問題

看板C_Sharp作者 (lostid)時間17年前 (2008/06/20 14:08), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串4/4 (看更多)
※ 引述《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

06/20 19:10, , 1F
if (ct is RadioButton) => better
06/20 19:10, 1F

06/22 15:42, , 2F
感謝
06/22 15:42, 2F
文章代碼(AID): #18MqbpZB (C_Sharp)
文章代碼(AID): #18MqbpZB (C_Sharp)