Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式将标签放入div或label中_C#_Asp.net - Fatal编程技术网

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

C# 以编程方式将标签放入div或label中,c#,asp.net,C#,Asp.net,我想将标签(或div)的值设置为元素而不是文本 例如,此代码创建一个复选框列表,如下所示: 第一个复选框 第二个复选框 第三个复选框 我需要在CodeBehind中创建我的列表,而不是直接在我尝试过的ASPX中: checkboxesList.Text = "<label for=\"one\"> < input type = \"checkbox\" id = \"one\" /> First checkbox </ label > <label

我想将标签(或div)的值设置为元素而不是文本

例如,此代码创建一个复选框列表,如下所示:


第一个复选框
第二个复选框
第三个复选框
我需要在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 >";
checkboxesList.Text=“第一个复选框第二个复选框”;
这样做,它只作为字符串打印,而不创建不同的标签。 如何从代码背后实现它,只需:

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

无需使用服务器端控件来生成所有HTML元素。可以直接将HTML用于容器元素。仅对应该可以从服务器代码访问的元素使用服务器端控件

<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>

属性用于将
与未放置在
中的
绑定。此外,IE不支持该属性。因此,在您的情况下,可以将复选框放置到标签中,而不是对
属性使用


您还需要知道,在一般情况下,HTML
id
属性不等于ASP
id
值。因此,如果您想使用
链接控件以获得
属性,那么通过属性设置
id
属性值。

不必使用服务器端控件来生成所有HTML元素。可以直接将HTML用于容器元素。仅对应该可以从服务器代码访问的元素使用服务器端控件

<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>

属性用于将
与未放置在
中的
绑定。此外,IE不支持该属性。因此,在您的情况下,可以将复选框放置到标签中,而不是对
属性使用


您还需要知道,在一般情况下,HTML
id
属性不等于ASP
id
值。因此,如果要使用
链接控件,请通过属性设置
id
属性值。

谢谢大家的回答

我最终决定使用复选框列表:

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

谢谢大家的回答

我最终决定使用复选框列表:

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

那么你有没有遇到任何错误或异常?@ershoaib没有,我现在的做法只是打印整个字符串尝试使用
asp:Literal
而不是
asp:Label
那么你有没有遇到任何错误或异常?@ershoaib没有,我这样做的方式只是打印完整的字符串尝试使用
asp:Literal
而不是
asp:Label
代码段生成的标记不是。标记定义了
元素的标签。您的标记将在一个标签中包含多个输入。由代码段生成的标记不是。标记定义了
元素的标签。标记将在一个标签中包含多个输入。