Domanda

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.

È stato utile?

Soluzione

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); 

Altri suggerimenti

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\">";

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.

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);
divBody.Controls.Add(rbl);

solved my problem and of course, that div has to have runat=server

Thank you.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top