tags:

views:

69

answers:

3

hey all,

i was wondering if any one could help me out, i have a table which looks something like the following

<table id="Table1" border="0">
    <tr>
        <td><b>1.</b> Question 1</td>
    </tr><tr>
        <td style="border-width:5px;border-style:solid;"></td>
    </tr><tr>
        <td align="left" style="width:1000px;"><input id="Radio1" type="radio" name="Group1" value="Radio1" /><label for="Radio1">Answer1</label></td>
    </tr><tr>
        <td align="left" style="width:1000px;"><input id="Radio1" type="radio" name="Group1" value="Radio1" /><label for="Radio1">Answer2</label></td>
    </tr><tr>
        <td align="left" style="width:1000px;"><input id="Radio1" type="radio" name="Group1" value="Radio1" /><label for="Radio1">Answer3</label></td>
    </tr><tr>
        <td align="left" style="width:1000px;"><input id="Radio1" type="radio" name="Group1" value="Radio1" /><label for="Radio1">Answer4</label></td>
    </tr><tr>
        <td style="height:30px;"></td>
    </tr><tr>
        <td><b>2.</b> Question 2</td>
    </tr><tr>
        <td style="border-width:5px;border-style:solid;"></td>
    </tr><tr>
        <td align="left" style="width:1000px;"><input id="Radio2" type="radio" name="Group2" value="Radio2" /><label for="Radio2">yes</label></td>
    </tr><tr>
        <td align="left" style="width:1000px;"><input id="Radio2" type="radio" name="Group2" value="Radio2" /><label for="Radio2">no</label></td>
    </tr><tr>
        <td style="height:30px;"></td>
    </tr>
</table>

how do i go about looping through each group of radio buttons and getting the text of the selected radio button ??

thanks a lot !!

the code displayed above is created dynamically ... in my aspx file i have the following code

   <asp:Table ID="Table1" runat="server">
        </asp:Table> 
A: 

Convert all controls to server controls (by adding the runat="server" attribute). You can then programatically access what you need o. The server.

Pierreten
the table is already set to runat server ... but all the radio buttons and groups are created dynamically ... so how do i go about changing all this to make sure all the controls which are created dynamically to runat server
c11ada
+2  A: 

If you want to access the rows in ASP.NET (on the server side), you need to convert the table, rows and the cells to server control (using runat="server") and iterate through the controls in the table.

EDIT : :- If you are adding the rows, cells and radionbuttons following way, all of them will be the server controls (and are runat=server) so that you can access them the way I mentioned above:--

// Create new row and add it to the table.
TableRow tRow = new TableRow();
table1.Rows.Add(tRow);
for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
{
// Create a new cell and add it to the row.   
TableCell tCell = new TableCell();
RadioButton rdb = new RadioButton();
rdb.ID = "rdb_" + cellCtr.ToString();
rdb.Text = "radio button";
rdb.GroupName = "rdbGroup";
tCell.Controls.Add(rdb);
tRow.Cells.Add(tCell);
}

EDIT:-

You can find the controls in each cell.Something like below:-

foreach(TableCell cell in tableRow.Cells)
{
      foreach(Control ctrl in cell.Controls)
      {
      if(ctrl is RadioButton)
      {
         if(ctrl.Selected)
          {
             string rdValue=ctrl.Text;
          }
      }
      }

}

Or If you want to iterate on the client side using Javascript, have a look here and you dont have to apply runat="server".

ydobonmai
the table is already set to runat server ... but all the radio buttons and groups are created dynamically ... so how do i go about changing all this to make sure all the controls which are created dynamically to runat server
c11ada
I just edited my answer. Have a look and let me know If that works for you.
ydobonmai
that is exactly how i am adding the radio buttons and the rows ... but the bit im stuck on is how do i loop through each one of the groups of radio buttons and get the text for each selected radio button ??
c11ada
I just edited my answer for some code to access the selected radiobutton text.
ydobonmai
A: 

It sounds like you're starting with a barebones <table> in your markup page, and dynamically adding those <input> afterwards.

Consider taking this approach:

  1. Add the runat="server" attribute to your table.
  2. In the code where you're adding those <input> tags, add a new RadioButton control. Use an ID here that you can predict later. Perhaps you can use a RadioButtonList instead, if the choices are logically grouped!
  3. It's unclear if you're manually adding those <tr> and <td> as strings. Consider the option of new TableRow() and new TableCell(). Then add the new RadioButton to the TableCell.Controls collection with tc.Controls.Add(myNewRadioButton);
  4. In your postback code, simply refer to your RadioButton controls by id, or even loop through the Controls collection property of the Table1.
foreach (Control x in Table1.Controls)
{
    if (x.GetType().ToString().Equals("System.Web.UI.WebControls.RadioButton"))
    {
         if (((RadioButton)x).Checked)
         {
             //proceed.
         }
    }
}
p.campbell
if (x is RadioButton) how do i get the text from the selected radiobutton ??
c11ada
@c11ada - Here's one way to determine if the RadioButton is selected.
p.campbell