ASP.NET DataList - defining “columns/rows” when repeating horizontal and using flow layout

喜夏-厌秋 提交于 2019-12-03 21:37:57

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();
        }
    }
}

I may be confused, but why not just use a repeater and a custom item template?

You can achieve your desired layout using CSS without altering the original generated markup. Since span tags are displayed inline by default, switching to inline-block and specifying a width of 33% on those spans should do the trick.

Or more specifically, set the CssClass property of your DataList control to a value such as "threecolumns".

Define the following css style:

<style type="text/css">
.threecolumns span 
{
    display: inline-block;
    width: 33%;
}
</style>

VS2008 might tell you that inline-block isn't a valid setting for the display property. I wouldn't worry about that too much since almost every browser supports it.

ivica

add this RepeatColumns="100000"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!