我可以在转发器中使用循环吗?推荐吗?

iTa*_*ayb 1 c# asp.net loops repeater

我的数据源有一个RatingdataItem包含一个0到5的整数.我想按顺序打印星号.

我想在Repeater控制范围内做到这一点:

<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" />
<%} %>
Run Code Online (Sandbox Code Playgroud)
  1. 我收到了错误The name 'Container' does not exist in the current context.这很奇怪,因为<%# DataBinder.Eval(Container.DataItem, "Name")%>之前我使用过一条线,效果很好.
  2. 在我的aspx页面中包含循环是否聪明?我觉得这不太方便.我有什么选择?
  3. #意味着什么?

非常感谢你.

Chr*_*tal 5

#指示时要执行的代码数据发生结合(即,当DataBind()被调用的控制或页).该<%# %>语法的数据绑定equivilent <%= %>所以很遗憾你不能只是换你的循环中<%# %>块和用它做.

您可以通过实现代码隐藏方法并将评级传递给方法来解决此限制:

<%# GetStars(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "Rating"))) %>
Run Code Online (Sandbox Code Playgroud)

然后实现方法为:

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;
}
Run Code Online (Sandbox Code Playgroud)