简体   繁体   English

以编程方式将标签放入div或label

[英]Programmatically put labels in div or label

I would like to set the value of my label (or div) as an element and not text. 我想将标签(或div)的值设置为元素而不是文本。

Example, this code creates a list of checkboxes as followed in this post How to use Checkbox inside Select Option : 例如,这段代码创建了一个复选框列表,如本文所述: 如何在Select Option中使用Checkbox

<asp:Label runat="server" id="checkboxesList">
  <label for="one">
     <input type="checkbox" id="one" />First checkbox</label>
  <label for="two">
    <input type="checkbox" id="two" />Second checkbox</label>
  <label for="three">
    <input type="checkbox" id="three" />Third checkbox</label>
</asp:Label>

I need to create my List in CodeBehind and not directly in ASPX I tried : 我需要在CodeBehind中创建我的列表,而不是直接在我尝试过的ASPX中创建:

checkboxesList.Text = "<label for=\"one\"> < input type = \"checkbox\" id = \"one\" /> First checkbox </ label > <label for= \"two\" > < input type = \"checkbox\" id = \"two\" /> Second checkbox </ label >";

Doing so it only prints as a string and do not create the differents labels. 这样做只会将其打印为字符串,而不会创建差异标签。 How to implement it from the code behind having just : 如何从刚刚实现的代码中实现它:

<asp:Label runat="server" id="checkboxesList">
</asp:Label>

It's not necessary to use server-side controls to generate all HTML elements. 不必使用服务器端控件来生成所有HTML元素。 It is possible to use HTML directly for container elements. 可以将HTML直接用于容器元素。 Use server-side controls only for elements that should be accessible from server code. 仅对应从服务器代码访问的元素使用服务器端控件。

<div id="checkboxes">
    <label>
        <asp:CheckBox ID="one" runat="server" Text="First checkbox" />
    </label>
    <label>
        <asp:CheckBox ID="two" runat="server" Text="Second checkbox" />
    </label>
    <label>
        <asp:CheckBox ID="three" runat="server" Text="Third checkbox" />
    </label>
</div>

The snippet allows you to get/set values and text of checkboxes on server-side. 该代码段允许您获取/设置服务器端复选框的值和文本。 Also you will be able to add server-side click event handler for each of the checkbox. 您还可以为每个复选框添加服务器端单击事件处理程序。

Note, the for attribute is used to bound the <label> with <input> that is not placed inside the <label> . 注意, for属性用于将<label>与未放置在<label>内的<label> <input>绑定。 Moreover the attribute is not supported by IE. 此外,IE不支持该属性。 So, in your case it is possible to place checkboxes into labels instead of using of for attribute. 所以,你的情况很可能将复选框到标签中,而不是使用的for属性。

Also you need to know the HTML id attribute in general case is not equal to ASP ID value. 另外,您还需要了解HTML id属性通常不等于ASP ID值。 Therefore, if you want to link controls using for attribute, then set id attribute value through ClientID property. 因此,如果要使用for属性链接控件,请通过ClientID属性设置id属性值。

Thank you all for the answers. 谢谢大家的回答。

I've finally decided to use a CheckBoxList : 我终于决定使用CheckBoxList:

<asp:Label runat="server" id="LabelList">
    <asp:CheckBoxList runat="server" ClientIDMode="Predictable" DataValueField="Value" DataTextField="Name" ID="specificInfoListControl" Width="150">
    </asp:CheckBoxList>
</asp:Label>

And I retrieve the values with : 我用以下方法检索值:

foreach (ListItem item in specificInfoListControl.Items)
{
  if (item.Selected)
  {
  }
}

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

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