简体   繁体   English

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

[英]c#.net how to get a HtmlGenricControll to show a RadioButtonList item

I have a HtmlGenericController and to this I want to add RadioButtons.我有一个 HtmlGenericController,我想添加 RadioButtons。 My radiobuttons come from a RadioButtonList an hence the objects are Listitems.我的单选按钮来自 RadioButtonList,因此对象是 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.要添加 label,您必须在旁边渲染一个span 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.或者,IIRC,渲染一个label字段并将for属性设置为单选按钮的 ID,因此单击 label 也会自动单击该按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM