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

发布于 2024-11-10 18:20:44 字数 1945 浏览 3 评论 0原文

我有一个 HtmlGenericController,我想添加 RadioButtons。我的单选按钮来自 RadioButtonList,因此对象是 Listitems。如何让我的通用控制器显示单选按钮?

这是我的代码

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;

        }

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;

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま柒月 2024-11-17 18:20:44
var radioButton = new HtmlGenericControl("input");
radioButton.Attributes["type"] = "radio";
radioButton.Attributes["name"] = "groupName";
radioButton.Attributes["value"] = "buttonValue";

不过,这只会渲染圆形单选按钮本身。要添加标签,您必须在其旁边渲染例如 span 。或者,IIRC,渲染一个 label 字段,并将 for 属性设置为单选按钮的 ID,这样单击标签也会自动单击该按钮。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文