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" />
<%} %>
- 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. - Is it clever to include loops in my
aspxpage? I think it’s not very convenient. What’s my alternatives? - What’s that
#means?
Thank you very much.
The
#indicates code to be executed when data-binding occurs (i.e. whenDataBind()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:
And then implement the method as: