我在页面上有一个复选框列表,如下所示。
<asp:CheckBoxList runat="server" ID="lstFeatures" RepeatDirection="Vertical"></asp:CheckBoxList>
后端代码看起来是这样的。
private void MakeRegionCheckboxes(ReportRegion region, int margin)
{
foreach (ReportRegion subregion in region.childRegions)
{
ListItem item = new ListItem();
item.Text = subregion.Name;
item.Value = subregion.Name;
item.Selected = subregion.DefaultSelection;
item.Attributes.Add("style", "margin-left:" + margin.ToString() + "px");
lstFeatures.Items.Add(item);
MakeRegionCheckboxes(subregion, margin + 30);
}
}
当它在一个空白元素上运行时,它会很好地缩进“子区域”,因为 style:margin-left:30px 在一个范围内呈现,如您所见。
<td>
<span style="margin-left:30px">
<input id="lstFeatures_1" type="checkbox" checked="checked" name="lstFeatures$1">
<label for="lstFeatures_1">Member Information</label>
</span>
</td>
但是,当我在主元素中运行相同 代码时,它不会呈现跨度,因此不会设置边距。我得到的就是这个。
<td>
<input id="ctl00_pg_BuildReport_lstFeatures_1" type="checkbox" checked="checked" name="ctl00$pg$BuildReport$lstFeatures$1">
<label for="ctl00_pg_BuildReport_lstFeatures_1">Member Information</label>
</td>
这两个元素 (3.5) 的框架相同,唯一的区别是主元素有一个母版页,也许还有一些额外的面板,但我只是想知道什么会阻止渲染的跨度?任何帮助都会很有用。谢谢!
最佳答案
试试看有没有效果:
item.Attributes.CssStyle.Add("margin-left", String.Format("{0}px", margin));
关于asp.net - 向 ListItem 添加属性,而不是呈现样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12666628/