c#.net如何获取HtmlGenricControll以显示RadioButtonList项

时间:2011-05-31 08:10:45

标签: c# radiobuttonlist htmlgenericcontrol

我有一个HtmlGenericController,我要添加RadioButtons。我的radiobutton来自RadioButtonList,因此对象是Listitems。如何让我的genericcontroller显示radiobuttons?

这是我的代码

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;

        }

1 个答案:

答案 0 :(得分:2)

var radioButton = new HtmlGenericControl("input");
radioButton.Attributes["type"] = "radio";
radioButton.Attributes["name"] = "groupName";
radioButton.Attributes["value"] = "buttonValue";

但这只会渲染圆形单片机本身。要添加标签,除了它之外,您还必须呈现span。或者,IIRC,渲染一个label字段,其for属性设置为单选按钮的ID,因此单击标签也会自动点击该按钮。