Reputation: 1762
i have a div with id "divBody"
RadioButtonList rbl = new RadioButtonList();
divBody.InnerHtml += rbl;
also rbl has items too, which is added by
rbl.Items.Add(new ListItem { Text = "asd", Value = "1" });
i cannot initialize divBody.InnerHtml += rbl;
because for that code piece, i see this as output in website : System.Web.UI.WebControls.ListItemCollection
this gotta be so easy to solve, but i don't want to initialize radiobuttonlist from .aspx page, i'd like to initialize this from .cs file.
Thank you for your patience.
Upvotes: 0
Views: 348
Reputation: 1762
divBody.Controls.Add(rbl);
solved my problem and of course, that div has to have runat=server
Thank you.
Upvotes: 0
Reputation: 125
RadioButtonList
is a server control and we cannot add it in InnerHTML of div control. You can do it as follows:
RadioButtonList rbl = new RadioButtonList();
rbl.Items.Add(new ListItem { Text = "asd", Value = "1" });
divBody.Controls.Add(rbl);
Upvotes: 0
Reputation: 621
I hope this helps.
divBody.Controls.Add(rbl);
When you use InnerHtml
, you have to set text to be rendered like below.
divBody.InnerHtml = "<input type=\"radio\" name=\"example\" value=\"foo\">";
Upvotes: 1
Reputation: 87
Since you set the runat="server" attribute for the div. Asp.Net will create a HtmlControl
Here you can instead of setting the InnerHtml property do the following:
divBody.Controls.Add(rbl);
This will render the radiobutton list during the rendering phase of the application lifecycle.
Upvotes: 0
Reputation: 9947
To add controls from code behind you don't need to play with html just add it
RadioButtonList rbl = new RadioButtonList();
rbl.Items.Add(new ListItem { Text = "asd", Value = "1" });
//also divbody should be a panel or div with runat = server
divBody.Controls.Add(rbl);
Upvotes: 1