如何在中获得替代样式(。网)?
我在这个网站上使用 EPiServer。与 asp:DataList
不同,EPiServer:PAgeList 没有 AlternatingItemTemplate
。
因此,我需要创建一个计数器并在我的
中增加该计数器,然后使用模数返回要附加到文章/页面的 css 样式。
模数“代码” - 来自代码后面:
return index % 2 == 0 ? "styleA" : "styleB";
但我无法在
中添加计数器并增加该计数器。
任何建议非常感谢!
更新
这是我的 EPiServer 页面列表控制器:
<EPiServer:PageList runat="server" id="pageList" SortDirection="Ascending" Count="4" OnDataBinding="pageList_OnDataBinding">
<HeaderTemplate>
<ul id="articleList1">
</HeaderTemplate>
<ItemTemplate>
<li>
<h2><a href="<%# Eval("LinkURL") %>" title="<%# Eval("PageName") %>"><EPiServer:Property id="Property1" PropertyName="PageName" runat="server" /></a></h2>
<div class="articleImage">
<%# ArticleImage(Container.CurrentPage)%>
</div>
<div class="introText">
<%# IntroText(Container.CurrentPage)%>
</div>
<div class="readMore floatRight"><a href="<%# Eval("LinkURL") %>" title="<%# Eval("PageName") %>">Les mer</a></div>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</EPiServer:PageList>
答案
我认为使用 jQuery 比使用 .NET 简单得多。 这不是我的首选解决方案,但它有效。我使用的代码是这样的:
if (jQuery("#articleList1").length > 0) {
jQuery('li:odd').addClass("odd");
}
I'm using EPiServer for this website. Unlike asp:DataList
, EPiServer:PAgeList does not have AlternatingItemTemplate
.
So I need to create a counter and increase this counter in my <ItemTemplate>
, and then use modulus to return whuch css style to append to article / page.
Modulus "code" - fromcode behind:
return index % 2 == 0 ? "styleA" : "styleB";
But I'm not abler to ad an counter and increase this in the <ItemTemplate>
.
Any suggestions much appreciated!
UPDATE
Here is my EPiServer Page List controller:
<EPiServer:PageList runat="server" id="pageList" SortDirection="Ascending" Count="4" OnDataBinding="pageList_OnDataBinding">
<HeaderTemplate>
<ul id="articleList1">
</HeaderTemplate>
<ItemTemplate>
<li>
<h2><a href="<%# Eval("LinkURL") %>" title="<%# Eval("PageName") %>"><EPiServer:Property id="Property1" PropertyName="PageName" runat="server" /></a></h2>
<div class="articleImage">
<%# ArticleImage(Container.CurrentPage)%>
</div>
<div class="introText">
<%# IntroText(Container.CurrentPage)%>
</div>
<div class="readMore floatRight"><a href="<%# Eval("LinkURL") %>" title="<%# Eval("PageName") %>">Les mer</a></div>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</EPiServer:PageList>
ANSWER
I decided that using jQuery was a LOT simpler than hacking around with .NET.
It's not my preferred solution, but it works. The code I use is this:
if (jQuery("#articleList1").length > 0) {
jQuery('li:odd').addClass("odd");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
对于中继器,我这样做:-
编辑 对于项目数据绑定事件,跟踪行计数器...
编辑 在这里... OOPS 需要一个 HtmlTableRow!
你还需要这个
For a repeater I do this:-
EDIT for an on item databound event keep a track of the row counter...
Edit here you go... OOPS needed to be a HtmlTableRow!!
you will also need this
如果您正在寻找的是替代行的一些视觉样式,您可能会发现使用 Javascript 和 jQuery 在客户端运行时操作样式会取得更好的成功。在某些情况下,您可以使用纯 CSS 来获得您想要的结果(但这可能会导致实现在不同浏览器中看起来不一样)。
如果您确实需要为交替行呈现不同的信息,则可能需要向要绑定的数据源添加一个属性以公开该信息。或者,某些控件支持
ItemDataBound
事件,您可以在服务器上订阅该事件并使用该事件来更改输出。编辑:
如果您选择订阅 ItemDatabound 事件(假设您的控件实际上具有此功能),您将计算一个递增值并将其分配给您要绑定的数据项。然后,您可以将其与模算术结合使用:
(count % 2)
为奇数/偶数行应用不同的样式。另一种替代方法是通过使用 ASP.NET 中的标记扩展来仅在标记中生成递增数字。这是一个带有中继器的示例:
If all you're looking for is some visual styling for alternate rows, You may find better success in using Javascript and jQuery to manipulate the styles at runtime on the client. In some cases, you can use pure CSS to get the result you want (but this can result in implementations that don't look the same in different browsers).
If you actually need to render different information for alternating rows, you may need to add a property to the data source you are binding to that exposes the information. Alternatively, some controls support a
ItemDataBound
event that you can subscribe to on the server and use to alter your output.EDIT:
If you choose to subscribe to the ItemDatabound event (assuming your control actually has this feature), you would compute an incrementing value and assign it to the data item you are binding to. You can then use this together with modular arithmetic:
(count % 2)
to apply different styles for odd/even rows.Another alternative is to hack things by using markup expansions in ASP.NET to generate an incrementing number just in the markup. Here's an example with a Repeater:
假设支持的浏览器是 IE9+,这可以使用纯 CSS 来完成。
This can be done with pure CSS assuming the supported browser is IE9+.
不幸的是,EpiServer“隐藏”了 Container 实例的 ItemIndex,但解决方法是创建您自己的“全局”计数器:
将属性添加到您的代码后面:
然后在您的 aspx 文件中:
Unfortunally EpiServer "hides" the ItemIndex of the Container instance but a work around is to create your own "global" counter:
Add a property to your code behind:
And then in your aspx file:
我的实现如下:
帮助我的是 Richard Everett answer。
详细答案:
这救了我的命!
I implemented it as below:
What helped me was Richard Everett answer.
The answer in detail:
This saved my day!
当我遇到服务器控件的此类问题时,我通常只是手动编写它们
When I run into these kinds of issues with server controls I usually just css them by hand
EPiServer PageList 控件还充当数据源,因此没有理由不能使用您最喜欢的 ASP.NET 模板化控件以及交替项来呈现它,只需将 DataSourceId 设置为页面列表的 ID 即可。
The EPiServer PageList control also functions as a DataSource, so there's no reason why couldn't use your favorite ASP.NET templated controls with alternating items to render it with, and simply set the DataSourceId to the ID of the pagelist.
这对我有用。 (感谢 @Rippo)
我刚刚使用了表格的
bgcolor
:This worked for me. (Credit to @Rippo)
I just used the
bgcolor
of the table: