ASP.NET DataList控件 - 定义"列/行"重复水平和使用流布局时 [英] ASP.NET DataList - defining "columns/rows" when repeating horizontal and using flow layout
问题描述
下面是我DataList控件:
Here is my DataList:
<asp:DataList id="DataList" Visible="false" RepeatDirection="Horizontal" Width="100%" HorizontalAlign="Justify" RepeatLayout="Flow" runat="server">
[Contents Removed]
</asp:DataList>
这生成具有包裹在一个跨度的每个项目的标记。从那里,我想每个跨度的闯入了三列行。理想情况下,我想是这样的:
This generates markup that has each item wrapped in a span. From there, I'd like to break each of these spans out into rows of three columns. Ideally I would like something like this:
<div>
<span>Item 1</span>
<span>Item 2</span>
<span>Item 3</span>
</div>
<div>
<span>Item 4</span>
<span>Item 5</span>
<span>Item 6</span>
</div>
[etc]
我能得到这个最接近的是设置RepeatColumns为3,然后一个&LT; BR&GT;
在DataList控件每三个项目后插入
The closest I can get to this is to set RepeatColumns to "3" and then a <br>
is inserted after every three items in the DataList.
<span>Item 1</span>
<span>Item 2</span>
<span>Item 3</span>
<br>
<span>Item 4</span>
<span>Item 5</span>
<span>Item 6</span>
<br>
这让我的样的的接近,但真的没有做的伎俩 - 我仍然无法控制布局,我想顺便能
This gets me kind of close, but really doesn't do the trick - I still can't control the layout the way I'd like to be able to.
任何人都可以提出一个方法,使这更好的?如果我能实现上面的例子 - 这将是完美的,但是我会接受一个不太优雅的解决方案,以及 - 只要它比更灵活&LT; BR&GT;
(例如插入&LT;跨度类=清除&GT;&LT; / SPAN&GT;
而不是&LT; BR&GT;
)。
Can anyone suggest a way to make this better? If I could implement the above example - that would be perfect, however I'd accept a less elegant solution as well - as long as its more flexible than <br>
(such as inserting a <span class="clear"></span>
instead of <br>
).
推荐答案
如果你真的需要使用一个DataList出于某种原因,而不是实现这个作为一个中继器,你可以尝试做这样的事情:
If you really needed to use a datalist for some reason instead of implementing this as a repeater, you can try doing something like this:
<asp:DataList ID="dataList" runat="server" RepeatDirection="Horizontal" Width="100%" HorizontalAlign="Justify" RepeatLayout="Flow" OnItemDataBound="dataList_ItemDataBound">
<HeaderTemplate>
<div>
</HeaderTemplate>
<SeparatorTemplate>
</div><div>
</SeparatorTemplate>
<ItemTemplate>
<%# Container.DataItem %></ItemTemplate>
<FooterTemplate>
</div></FooterTemplate>
</asp:DataList>
protected void dataList_ItemDataBound(object sender, DataListItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Separator) {
if ((e.Item.ItemIndex + 1) % 3 != 0) {
e.Item.Controls.Clear();
}
}
}
这篇关于ASP.NET DataList控件 - 定义&QUOT;列/行&QUOT;重复水平和使用流布局时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!