Generating an aspx <div> content from C# code behind

709 views Asked by At

I'm developing an ASPX web page on Visual Studio 2013 and I have the follow code:

<div id="Q1" style="width: 100%; text-align:justify">
    <div style="float: left; width: 70%; align-items: center; background-color:rgba(99, 99, 99, 0.40); padding-top: 5px;padding-left: 5px;padding-bottom: 5px;padding-right: 5px;">
        <asp:Label ID="LabelM12" runat="server"></asp:Label>
    </div>
    <div style="float: left; width: 25%; text-align:center; align-items:center">
        <asp:RadioButtonList ID="RadioButtonListM12" runat="server" RepeatDirection="Horizontal" RepeatLayout="Table" onclick="ShowTxt('1');">
        <asp:ListItem Text = "Yes" Value = "Yes"></asp:ListItem>
        <asp:ListItem Text = "No" Value = "No"></asp:ListItem>
        <asp:ListItem Text = "NA" Value = "NA"></asp:ListItem>
    </asp:RadioButtonList>
    </div>
</div>

I need to build a check list repeating the code above 67 times!

How can I create the complete div id="Q1" content from C# code behind?, something like:

for(int i = 0; i < 67; i++)
{
     //Code to generate the div id="Q1" content
}

I hope you can help me, Thanks in advance!

1

There are 1 answers

0
Tim Schmelter On BEST ANSWER

So you want to repeat following 67 times:

<div style="width: 100%; text-align:justify">
    <div style="float: left; width: 70%; align-items: center; background-color:rgba(99, 99, 99, 0.40); padding-top: 5px;padding-left: 5px;padding-bottom: 5px;padding-right: 5px;">
        <asp:Label ID="LabelM12" runat="server"></asp:Label>
    </div>
    <div style="float: left; width: 25%; text-align:center; align-items:center">
        <asp:RadioButtonList ID="RadioButtonListM12" runat="server" RepeatDirection="Horizontal" RepeatLayout="Table" onclick="ShowTxt('1');">
        <asp:ListItem Text = "Yes" Value = "Yes"></asp:ListItem>
        <asp:ListItem Text = "No" Value = "No"></asp:ListItem>
        <asp:ListItem Text = "NA" Value = "NA"></asp:ListItem>
    </asp:RadioButtonList>
    </div>
</div>

Simple. Put it in a UserControl and add 67 instances of it to a panel.

for(int i = 0; i < 67; i++)
{
    MyRadioButtonListControl c = (MyRadioButtonListControl)LoadControl("MyRadioButtonListControl.ascx");
    myPanel.Controls.Add(c);
}