c#.net how to get a HtmlGenricControll to show a RadioButtonList item
I have a HtmlGenericController and to this I want to add RadioButtons. My radiobuttons come from a RadioButtonList an hence the objects are Listitems. How do I get my 开发者_开发百科genericcontroller to show the radiobuttons?
This is my code
private HtmlGenericControl generateCells(String domainName)
{
HtmlGenericControl container = new HtmlGenericControl("div");
HtmlGenericControl dName = new HtmlGenericControl("span");
dName.InnerHtml = domainName;
RadioButtonList radioList = new RadioButtonList();
radioList.ID = "radio_" + domainName;
radioList.RepeatDirection = RepeatDirection.Horizontal;
ListItem sunriseA = new ListItem();
sunriseA.Value = Price_Types.SUNRISE_ONE.ToString();
//sunriseA.Text = Price_Types.SUNRISE_ONE.ToString();
sunriseA.Text = "";
radioList.Items.Add(sunriseA);
ListItem sunriseB = new ListItem();
sunriseB.Value = Price_Types.SUNRISE_TWO.ToString();
//sunriseB.Text = Price_Types.SUNRISE_TWO.ToString();
sunriseB.Text = "";
radioList.Items.Add(sunriseB);
ListItem landrush = new ListItem();
landrush.Value = Price_Types.LANDRUSH.ToString();
//landrush.Text = Price_Types.LANDRUSH.ToString();
landrush.Text = "";
radioList.Items.Add(landrush);
ListItem general = new ListItem();
general.Value = Price_Types.GENERAL.ToString();
//general.Text = Price_Types.GENERAL.ToString();
general.Text = "";
radioList.Items.Add(general);
container.Controls.Add(dName);
foreach (ListItem item in radioList.Items)
{
HtmlGenericControl span = new HtmlGenericControl("span");
span.InnerHtml = item;//what to put here??
container.Controls.Add(span);
}
return container;
}
var radioButton = new HtmlGenericControl("input");
radioButton.Attributes["type"] = "radio";
radioButton.Attributes["name"] = "groupName";
radioButton.Attributes["value"] = "buttonValue";
That will only render the round radiobutton itself, though. To add a label, you'll have to render for example a span
besides it. Or, IIRC, render a label
field with the for
attribute set to the ID of the radiobutton, so clicking the label will automatically click the button too.
精彩评论