我可以使用循环直放站?难道是建议? [英] Can I use loops in repeater? Is it recommended?

查看:161
本文介绍了我可以使用循环直放站?难道是建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据源有一个评级的DataItem包含从0到5的整数,我想accordignly打印的明星。

我试图转发控制范围内做到这一点:

 < B>评级:其中; / B><%为(INT J = 1; J< =的DataBinder.Eval(的Container.DataItem,评级); J ++)
{%GT;
&所述; IMG SRC =App_Pics / fullstar.png/>
<%}
对于(INT J = 1; J< = 5 - DataBinder.Eval的(的Container.DataItem,评级); J ++)
{%GT;
&所述; IMG SRC =App_Pics / emptystar.png/>
<%}%GT;


  1. 我的错误'集装箱'这个名字并不在目前的情况下存在。这是奇怪,因为当我用<%#DataBinder.Eval的(的Container.DataItem,姓名)%方式> 前行,它的工作伟大

  2. 它是聪明的,包括在我的 ASPX 页循环?我认为这是不是很方便。什么是我的选择呢?

  3. 那是什么意思?

非常感谢你。


解决方案

表示要执行code时的数据绑定发生(即当的DataBind()被称为在控制或页)。在<%#%> 语法是&LT数据绑定equivilent;%=%> 等等不幸的是,你不能只是换你的循环在<%#%方式> 块,并用它做

您可以绕过这个限制通过实施code隐藏方法并传递评级方法:

 <%#GetStars(Convert.ToInt32(DataBinder.Eval的(的Container.DataItem,等级)))%GT;

,然后实现方法如下:

 保护字符串GetStars(INT等级)
{
    字符串输出=的String.Empty;
    对于(INT J = 1; J< =评级; J ++)输出+ =< IMG SRC = \\App_Pics / fullstar.png \\/>中;
    对于(INT J = 1; J< = 5 - 评级; J ++)输出+ =< IMG SRC = \\App_Pics / emptystar.png \\/>中;
    返回输出;
}

My datasource has an Rating dataItem contains an integer from 0 to 5. I'd like to print stars accordignly.

I'm trying to do it within Repeater control:

<b>Rating:</b>

<% for (int j = 1; j <= DataBinder.Eval(Container.DataItem, "Rating"); j++)
{  %>
<img src="App_Pics/fullstar.png" />
<% }
for (int j = 1; j <= 5 - DataBinder.Eval(Container.DataItem, "Rating"); j++)
{ %>
<img src="App_Pics/emptystar.png" />
<%} %>

  1. I get the error The name 'Container' does not exist in the current context. It is weird, because when I used <%# DataBinder.Eval(Container.DataItem, "Name")%> a line before, it worked great.
  2. Is it clever to include loops in my aspx page? I think it's not very convenient. What's my alternatives?
  3. What's that # means?

Thank you very much.

解决方案

The # indicates code to be executed when data-binding occurs (i.e. when DataBind() is called on the control or the page). The <%# %> syntax is the data-binding equivilent of <%= %> so unfortunately you can't just wrap your loop in <%# %> blocks and be done with it.

You can around this limitation by implementing a code-behind method and passing the rating to the method:

<%# GetStars(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "Rating"))) %>

And then implement the method as:

protected string GetStars(int rating)
{
    string output = string.Empty;
    for (int j = 1; j <= rating; j++) output += "<img src=\"App_Pics/fullstar.png\" />";
    for (int j = 1; j <= 5 - rating; j++) output += "<img src=\"App_Pics/emptystar.png\" />";
    return output;
}

这篇关于我可以使用循环直放站?难道是建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆