我需要根据Container.ItemIndex % 2 ==0
条件
但对我来说,它总是根据代码中的编码返回true或false
<asp:Repeater ID="rptNews" runat="server" EnableViewState="False" OnItemDataBound="rptNews_ItemDataBound">
<ItemTemplate>
<div style="display:none;"><%# _repeaterIndex = Container.ItemIndex %></div>
<% if (_repeaterIndex % 2 == 0)
{%>
<div class="col-md-4 col-lg-3 col-sm-6 no-padding newslist-col">
<div class="blog-item style-3">
<div class="blog-image">
<img alt="image" class="img-responsive center-block" src='<%# Eval("Image") %>' />
</div>
<div class="blog-content">
<div class="text-center">
<h5 class="date"><%# getDate(Eval("Date")) %></h5>
<h3 class="title-news blog-title newslisttitle"><%# Eval("Heading") %></h3>
<a href='<%# getURL(Eval("ID")) %>'><strong> MORE </span></strong></a>
</div>
</div>
</div>
</div>
<% }
else
{ %>
<div class="col-md-4 col-lg-3 col-sm-6 no-padding newslist-col">
<div class="blog-item style-3">
<div class="blog-content content-top">
<div class="text-center">
<h5 class="date"><%# Eval("Date") %></h5>
<h3 class="title-news blog-title newslisttitle"><%# _repeaterIndex %> <%# Eval("Heading") %></h3>
<a href='<%# getURL(Eval("ID"),Eval("Heading")) %>'><strong> MORE </strong></a>
</div>
</div>
<div class="blog-image">
<img alt="image" class="img-responsive center-block" src='<%# Eval("Image") %>' />
</div>
</div>
</div>
<% } %>
</ItemTemplate>
</asp:Repeater>
代码背后
protected void rptNewsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
_repeaterIndex = _repeaterIndex + 1;
if (e.Item.ItemIndex % 2 == 0)
{
_showTop = true;
}
else
{
_showTop = false;
}
}
}
到目前为止,我已经尝试了几件事,但它无法正常工作if (_repeaterIndex % 2 == 0)
根据总行数计算,这总是真或假,因为在我的情况下它始终显示13
因为我有13记录,如果我显示测试_repeaterIndex
它显示正确的值,但是如果它始终取最终值Container.ItemIndex
而不是当前值
<h3 class="title-news blog-title newslisttitle"><%# _repeaterIndex%> <%# Eval("Heading") %></h3>
解决这个问题的最佳方法是什么?
答案 0 :(得分:1)
直接使用Container.ItemIndex
而不是使用ItemDataBound
事件会更容易。您可以使用两个PlaceHolders并根据ItemIndex的值设置其可见性。
<ItemTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# Container.ItemIndex % 2 == 0 %>'>
Even content<br />
</asp:PlaceHolder>
<asp:PlaceHolder ID="PlaceHolder2" runat="server" Visible='<%# Container.ItemIndex % 2 == 1 %>'>
Odd content<br />
</asp:PlaceHolder>
</ItemTemplate>
答案 1 :(得分:0)
花了好几个小时试图寻找解决这个问题的其他方法后,我终于使用了两个Panel而我隐藏了&amp;显示基于后面代码中的e.Item.ItemIndex % 2 == 0
值。我认为这是最有效的方法。
<asp:Repeater ID="rptNewsList" runat="server" EnableViewState="False" OnItemDataBound="rptNewsList_ItemDataBound">
<ItemTemplate>
***HTML WILL GO HERE
<asp:Panel runat="server" ID="PanelTopImage" CssClass="col-md-4 col-lg-3 col-sm-6 no-padding newslist-col">
</asp:Panel>
<asp:Panel runat="server" ID="PanelBottomImage" CssClass="col-md-4 col-lg-3 col-sm-6 no-padding newslist-col">
***HTML WILL GO HERE
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
代码背后
protected void rptNewsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Panel pnlTopImage = e.Item.FindControl("PanelTopImage") as Panel;
Panel pnlBottomImage = e.Item.FindControl("PanelBottomImage") as Panel;
if (e.Item.ItemIndex % 2 == 0)
{
pnlTopImage.Visible = true;
pnlBottomImage.Visible = false;
}
else
{
pnlTopImage.Visible = false;
pnlBottomImage.Visible = true;
}
}
}