Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Help with Repeater Control

Author  Topic 

Markuu
Starting Member

9 Posts

Posted - 2007-11-09 : 22:22:59
Is there any way to change the settings on the repeater control so that instead of simply having just

<ItemTemplate>
</ItemTemplate>
<AlternatingItemTemplate>
</AlternatingItemTemplate>

which alternates rows, I could somehow make it change every 7 rows?

For example, I have a list of about 200 entries, but only one entry per day. I would therefore want to list 7 entries with one background color, and then the next 7 with a different background color, and then rotate between those to distinguish to my users which entries belong to the same week.

Any help would be appreciated!

Thank you in advance!!

JBelthoff
Posting Yak Master

173 Posts

Posted - 2007-11-10 : 09:55:08
Repeater Controls are very basic as are Most Controls. But writing your own output display is really very simple too. I have this which uses a Placeholder. Then from code behind I build a string, I put that string into a Literal Control and then add that Literal Control to the Placeholder. This way you can do anything you want.

It might look intimidating but it's really not. I simply add a css class to the table row if the day = 2. You could also use a switch statement (Select Case in VB) for 7 different days. alt1, alt2 etc...


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
table{border: solid 1px #222;border-collapse: collapse;}
th, td{border: solid 1px #222;}
.alt{background-color: #ddd;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>


using System;
using System.Text;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class test : System.Web.UI.Page
{
protected List<DataHolder2> l;

protected void Page_Load(object sender, EventArgs e)
{
l = LoadData();

StringBuilder str = new StringBuilder();
str.AppendLine("<table>");
str.AppendLine(" <tr>");
str.AppendLine(" <th>Name</th>");
str.AppendLine(" <th>Day</th>");
str.AppendLine(" </tr>");

for (Int32 x = 0; x < l.Count; x++)
{
if (l[x].Day == 1)
str.AppendLine(" <tr>");
else
str.AppendLine(" <tr class=\"alt\">");

str.Append(" <td>");
str.Append(l[x].Name);
str.AppendLine("</td>");

str.Append(" <td>");
str.Append(l[x].Day);
str.AppendLine("</td>");

str.AppendLine(" </tr>");

}

str.AppendLine("</table>");

Literal lit1 = new Literal();
lit1.Text = str.ToString();
PlaceHolder1.Controls.Add(lit1);

}

private List<DataHolder2> LoadData()
{
l = new List<DataHolder2>();

for (Int32 x = 0; x <= 20; x++)
{
DataHolder2 dh = new DataHolder2();
if (x < 10)
dh.Day = 1;
else
dh.Day = 2;
dh.Name = "John";
l.Add(dh);
}

return l;
}
}
public class DataHolder2
{
private Int32 _day;
public Int32 Day
{
get { return this._day; }
set { this._day = value; }
}

private String _name;
public String Name
{
get { return this._name; }
set { this._name = value; }
}
}


JBelthoff
• Hosts Station is a Professional Asp Hosting Provider
› As far as myself... I do this for fun!
Go to Top of Page

Markuu
Starting Member

9 Posts

Posted - 2007-11-10 : 13:07:39
Wow, that was a very informative answer! Thanks a lot for your help!
Go to Top of Page
   

- Advertisement -