HtmlInputCheckBox和标签为=(HtmlInputCheckBox and Label

2019-10-22 01:35发布

说我有下面的HTML,我想从一个C#的WebForms应用程序背后的代码生成。 如何创建复选框的标签吗?

HTML

<div class="col-lg-8 col-lg-offset-1">
    <input id="combo" type="checkbox" value="false" />
    <label for="combo" class="margin">Text Here</label>
</div>

后面的代码

private static HtmlGenericControl CreateHtmlInputCheckBoxControl(Filters filter)
{
    HtmlGenericControl column = CreateColumn(filter);
    column.Controls.Add(new HtmlInputCheckBox { Checked = false, Value = filter.Caption });
    return column;
}

private static HtmlGenericControl CreateColumn(Filters filter, string additionalClasses = "")
{
    HtmlGenericControl div = new HtmlGenericControl("div");
    div.Attributes["class"] = string.Format("col-lg-{0} col-lg-offset-{1} {2}", filter.ColumnSpan, filter.ColumnOffset, additionalClasses);
    return div;
}

在这种方法中增加对可能的标签或使用文字只有这样,才能做到这一点?

Answer 1:

OK,你可以将标签添加到容器中,并通过控制设置为=属性如下集合。

private static HtmlGenericControl CreateHtmlInputCheckBoxControl(Filters filter)
{
    HtmlGenericControl column = CreateColumn(filter);
    column.Controls.Add(new HtmlInputCheckBox { ID = string.Format("{0}{1}", filter.ID, filter.ReportID), Checked = false });

    HtmlGenericControl l = new HtmlGenericControl("label"){InnerText=filter.Caption};       
    l.Attributes["for"]= string.Format("{0}{1}", filter.ID, filter.ReportID);
    column.Controls.Add(l);

    return column;
}


Answer 2:

您可以使用标签控制

Label l = new Label
{
  Text = filter.Caption,
  AssociatedControlID = string.Format("{0}{1}", filter.ID, filter.ReportID)
};
column.Controls.Add(l);

https://msdn.microsoft.com/en-us/library/system.windows.forms.label(v=vs.110).aspx



文章来源: HtmlInputCheckBox and Label for=