2014年6月4日 星期三

ASP.NET Foreach RadioButton

ASP.NET Foreach RadioButton



HTML:
<asp:Panel ID="pan_RadioButton1" runat="server">
<asp:RadioButton ID="rbtempty" runat="server" GroupName="ProjectType" Text="Empty project without any defaults." /><br />
<asp:RadioButton ID="rbtcopy" runat="server" GroupName="ProjectType" Text="Copy an existing project." />&nbsp;
<asp:DropDownList ID="ddlcopymodel" runat="server" Width="100"></asp:DropDownList>
</asp:Panel>
<asp:Button ID="btnSave" runat="server" Text="Save" Width="100" OnClientClick="return confirm('Do you really want to save?');" OnClick="btnSave_Click" />
<asp:Button ID="btnprojecttypecancel" runat="server" Text="Cancel" Width="100" />

C#:
protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string r_id = "";
                string r_value = "";
             
                foreach (Control ctrl in pan_RadioButton1.Controls)//pan_RadioButton1這是Panel物件,取出Panel中所有物件
                {
                    if (ctrl.GetType().Name == "RadioButton") //判斷物件是否為RadioButton
                    {
                        RadioButton rbt = ctrl as RadioButton; //將ctrl建立成RadioButton物件
                        if (rbt.Checked) //判斷RadioButton是否被選取
                        {
                            r_id = rbt.ID.ToString();
                            r_value = rbt.Text;
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

沒有留言:

張貼留言