Search code examples
c#asp.netvisual-studio-2008ascx

How to dynamically generate html with .ascx?


I have some settings stored in web.config like this:

<add key="Answers" value="radiobutton1,radiobutton2,radiobutton3"/>

Radiobutton1, radiobutton2 and radiobutton3 are radiobutton label values.

In settings.cs I have a function to retrieve value from web.config:

    public static string Answers
    {
        get
        {
            return System.Configuration.ConfigurationManager.AppSettings["Answers"];
        }
    }

.ascx file:

<table runat="server" OnPreRender="Radio_PreRender" id="table1" name="table1">

</table>

My ascx.cs file contains this function:

protected void Radio_PreRender(object sender, EventArgs e)
{
    if (Settings.Answers != "")
    {
        int counter = 0;
        string a = Settings.Answers;
        string[] words = a.Split(',');

        StringWriter stringwriter = new StringWriter();
        HtmlTextWriter writer = new HtmlTextWriter(stringwriter);

        foreach (string word in words)
        {
            writer.WriteBeginTag("tr");
            writer.WriteBeginTag("td");
            writer.Write("abc123");

            RadioButton rdb1 = new RadioButton();
            rdb1.Checked = true;
            rdb1.GroupName = "rdbgroup";
            rdb1.ID = "radiobutton" + counter;
            rdb1.Text = word;
            table1.Controls.Add(rdb1 );

            writer.WriteEndTag("td");
            writer.WriteBeginTag("tr");
            table1.Render(writer);   
            counter++;
        }
    }
}

In other words, I want to generate a dynamic number of this code inside table1:

<tr>
  <td>
    // input type="radiobutton" and label go here.
  </td>
</tr>

At the moment radiobuttons are not generated, because they can't be direct child elements to a table. If I specify a div instead, radiobuttons are generated, but everything I try to write with HtmlTextWriter is not. I understand that my html has to be rendered by using table1.Render(writer); or something similar, but I can't figure it out.


Solution

  • You can try creating a table and add it to the page you are working on, using the exmaple below you can replace the textbox with a radiobutton

    Here is an example:

     //Creat the Table and Add it to the Page
                Table table = new Table();
                table.ID = "Table1";
                Page.Form.Controls.Add(table);
                // Now iterate through the table and add your controls 
                for (int i = 0; i < rowsCount; i++)
                {
                    TableRow row = new TableRow();
                    for (int j = 0; j < colsCount; j++)
                    {
                        TableCell cell = new TableCell();
                        TextBox tb = new TextBox();
    
                        // Set a unique ID for each TextBox added
                        tb.ID = "TextBoxRow_" + i + "Col_" + j;
    
                        // Add the control to the TableCell
                        cell.Controls.Add(tb);
                        // Add the TableCell to the TableRow
                        row.Cells.Add(cell);
                    }
                    // Add the TableRow to the Table
                    table.Rows.Add(row);
                }