Search code examples
c#asp.netradio-buttonfindcontrol

Retrieve value of dynamic radio button controls in asp:table


I am creating radio button dynamically inside asp:table. iI want to get the id of the selected radio button.

  1. How to check the control type
  2. How to get its ID.

        RadioButton radioButton = new RadioButton();
        radioButton.ID = id + "RadioButton";
        radioButton.ToolTip = text;
        radioButton.Attributes.Add("id", id);
        radioButton.GroupName = categoryID + "Questions";
        radioButton.EnableViewState = true;
        radioButton.AutoPostBack = true;
        radioButton.Checked = isSelected;
        radioButton.CssClass = style;
    
        TableCell cell = new TableCell();
        cell.HorizontalAlign = HorizontalAlign.Left;
        cell.Controls.Add(radioButton);
        TableRow row = new TableRow();
        row.Cells.Add(cell);
        table.Rows.Add(row);
    

Solution

  • Solution. i am posting this solution to help others.

            RadioButton rb = new RadioButton();            
            foreach (TableRow tr in QuestionTable.Controls)
            {
                foreach (TableCell tc in tr.Controls)
                {
                    if (tc.Controls[0] is RadioButton)
                    {
                        rb = (RadioButton)tc.Controls[0];
                        if (rb.Checked)
                        {
                            string aa = rb.ID;
                        }
                        break;
                    }
                }               
            }