0

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.

4

2 回答 2

2

您可以尝试创建一个表格并将其添加到您正在处理的页面中,使用下面的示例您可以用单选按钮替换文本框

这是一个例子:

 //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);
            }
于 2012-10-07T09:52:15.467 回答
1

我经常通过在整个 asp.net 生命周期中运行用户控件并将其作为字符串获取来执行此操作:

http://www.diaryofaninja.com/blog/2009/09/14/a-simple-solution-to-viewing-a-preview-of-any-page-in-your-site

这样您就可以将 ASP.Net 用作任何东西的模板引擎

Page tempPage = new Page();
UserControl myUserControl = new MyUserControl();
tempPage.Controls.Add(myUserControl);


StringWriter sw = new StringWriter();
HttpContext.Current.Server.Execute(tempPage, sw, false);
if (!String.IsNullOrEmpty(sw.ToString()))
{
    return sw.ToString();
}
于 2012-10-07T10:00:50.663 回答