How to dynamically generate html with .ascx?

LAngelessunsh

New Member
I have some settings stored in web.config like this:\[code\]<add key="Answers" value="http://stackoverflow.com/questions/12767712/radiobutton1,radiobutton2,radiobutton3"/>\[/code\]Radiobutton1, radiobutton2 and radiobutton3 are radiobutton label values.In settings.cs I have a function to retrieve value from web.config:\[code\] public static string Answers { get { return System.Configuration.ConfigurationManager.AppSettings["Answers"]; } }\[/code\].ascx file:\[code\]<table runat="server" OnPreRender="Radio_PreRender" id="table1" name="table1"></table>\[/code\]My ascx.cs file contains this function:\[code\]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++; } }}\[/code\]In other words, I want to generate a dynamic number of this code inside table1:\[code\]<tr> <td> // input type="radiobutton" and label go here. </td></tr>\[/code\]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.
 
Back
Top