ASPX question regarding Repeater controls

PARANOiA

Veteran
Hello

I'm starting a new site in which I'm looping through an ASPX repeater, pulling values from a database and formatting them. It's a vegetarian restaurant review site, if you're interested :). I rank sites from 1 to 5, and want to draw an appropriate number of stars on the site based in the integer stored in the database.

My problem comes in that while I can extract the numberic value from the control, eg
Code:
="<%#DataBinder.Eval(Container.DataItem, "Ranking")%>
I can't use this in a loop, to say, do this:
Code:
<% for i = 1 to DataBinder.Eval(Container.DataItem, "Ranking")
        Response.WriteLine(*)
    next

Is this possible? I can't seem to find this specific issue on the net. Or am I going the completely wrong way about this.

Thanks in advance.
 
You could try something like this (sorry that the code is mainly in C#):

Your repeater's itemtemplate should look something like this at the moment:
Code:
			<asp:Repeater id="Repeater1" runat="server">
				<ItemTemplate>
					<asp:Label id="myLabel" runat="server">
						<%#DataBinder.Eval(Container.DataItem, "Number")%>
						<br />
					</asp:Label>
				</ItemTemplate>
			</asp:Repeater>

Now, we're going to use repeater's ItemDataBound-event to change the output. How to do this depends if you're using codebehind or not. If you use, I then presume that your also using Visual Studio.NET. In that case, just select the repeator and click its ItemDataBound-event. Then skip the next part. If you're not using codebehind, change the first line of the previous code to look like this (with VS.NET you can skip this one):
Code:
<asp:Repeater id="Repeater1" runat="server" OnItemDataBound="myRepeater_ItemDataBound">

Then add this code:
Code:
		private void myRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				System.Data.Common.DbDataRecord dr = (System.Data.Common.DbDataRecord)e.Item.DataItem;
				int number = dr.GetInt32(1);
				Label label = (Label)e.Item.FindControl("myLabel");
				if (number == 2)
				{
					label.Text = "**";
				}
			}
		}

So, we first check that the itemtype is correct. After that we create a new DbDataRecord-object from the parameter 'e'. We know that the right column is the second one and its type is int, so we read that value into a new int-variable. Then we find the label that is supposed to show the number, so you have to replace the "myLabel" with the Label id you have in the repeator. Then we just check the value of the integer and draw as many stars as needed.

In VB.NET it would go something like this:


Code:
Private Sub myRepeater_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs)
   If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
      Dim dr As System.Data.Common.DbDataRecord = CType(e.Item.DataItem, System.Data.Common.DbDataRecord)
      Dim number As Integer = dr.GetInt32(1)
      Dim label As Label = CType(e.Item.FindControl("myLabel"), Label)
      If number = 2 Then
         label.Text = "**"
      End If
   End If
End Sub 'myRepeater_ItemDataBound

Hopefully this helps.
 
This looks to be exactly what I'm looking for - thank you so much !

I can't test it now as I just booted Linux for the first time on an install - however, I did manage to get a working solulution throwing the data using function. But obviously that was a pretty dodgy solution ! Yours looks like it's a much more graceful solution.

thanks again
 
Back
Top