ASP.net access controls dynamically
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>
</as开发者_运维百科p: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 ??
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?)
精彩评论