views:

112

answers:

1

hey all,

i have a table which looks similar to this

<asp:TableRow><asp:TableCell>Question 1</asp:TableCell><asp:TableCell ID ="Question1Text"></asp:TableCell></asp:TableRow>
        <asp:TableRow><asp:TableCell ColumnSpan="2">
            <asp:RadioButtonList ID="RadioButtonList1" runat="server"><asp:ListItem>Yes</asp:ListItem><asp:ListItem>No</asp:ListItem>
            </asp:RadioButtonList>
        </asp:TableCell></asp:TableRow>
        <asp:TableRow><asp:TableCell>
            <asp:TextBox ID="TextBox1" TextMode="MultiLine" runat="server"></asp:TextBox></asp:TableCell></asp:TableRow>
        <asp:TableRow><asp:TableCell>Question 2</asp:TableCell><asp:TableCell ID ="Question2Text"></asp:TableCell></asp:TableRow>
               <asp:TableRow><asp:TableCell ColumnSpan="2">
            <asp:RadioButtonList ID="RadioButtonList2" runat="server"><asp:ListItem>Yes</asp:ListItem><asp:ListItem>No</asp:ListItem>
            </asp:RadioButtonList>
        </asp:TableCell></asp:TableRow>
        <asp:TableRow><asp:TableCell>
            <asp:TextBox ID="TextBox2" TextMode="MultiLine" runat="server"></asp:TextBox></asp:TableCell></asp:TableRow>

i want to be able to systematically acces table cells with ID's for example

for (int i = 1; i<3 ; i++)
{
// i want to be able to access the table cell with the ID Question1Text then Question2Text and so on 
}

is this even possible ??

+1  A: 
for(int i = 0; i < 3; i++)
{
    string id = string.Format("Question{0}Text", i);
    TableCell cell = (TableCell) FindControl(id);

    // do whatever you want with the cell
}

You can use FindControl to search a control tree for controls with the given identifier string.

Though I must say, I don't think I would write code like that. I would simply define a regular HTML table, and then have <asp:Literal> controls where I needed to specifically control the text. What you've got here is rather difficult for me to parse and understand (is it generated by the designer, maybe?)

Dean Harding
this code was written by me, the only reason I want to do it this way is that the questions are stored in a database, i have about 30 questions and i thought it would be easier to dynamically add the question text in !! i want to create some sort of feedback from, if you have any ideas to make it easier that would be great !!
c11ada
@c11ada: look at the data bound controls, such as the DataList and Repeater.
AMissico