How can I achieve Rowspan capabilities using the ASP:Repeater control

E

Eric

Hello,
I have the following dataset that I want to bind to a repeater to be
displayed as a table.

Owner Animal Volume
---------------------------
Eric Dog 6
Eric Cat 3
Eric Gerbil 1
Eric Bird 10
Jenny Dog 2
Jenny Cat 4

I'd like to have an ASP repeater return a table that looks like the
following.
Specifically not adding the "Owner" over and over again. I'd like the
owner to shop up on the first entry only.
_____________________________
|Owner Animal Volume |
-----------------------------
|Eric Dog 6 |
| Cat 3 |
| Gerbil 1 |
| Bird 10 |
|Jenny Dog 2 |
| Cat 4 |
-----------------------------

I've been messing with this for a while but I have a feeling that the
repeater isn't capable of doing this. This is easy enough in "old
school" ASP but I'm having trouble getting it to work in ASP.net.

Any help would be greatly appreciated.

Thanks

Eric
 
A

Aaron Lewis

Go to HTML view of the ASPX, then hop in between your asp:Repeater
element tags and create a <HeaderTemplate>. In there, put your starting
table element and the first row (or tableheader, whichever you want).
Create a <FooterTemplate> closing out the table tags you put in the
<HeaderTemplate>. The IDE isn't smart enough to figure out what you're
doing, so depending on your build warning level, you may see warnings in
your output whining about a missing close tag for the table (but it'll
still compile). In the <ItemTemplate>, put table rows for your data.
<tr><td><%# DataBinder.Eval(Container,
 
E

Eric

hmmmm.....I've gotten this far already but it's still showing multiple
entries for Eric and Jenny. I want to get a rowspan effect that only
shows the Owners column value for the first row that appears. I don't
see how this solution alllows for the addition of rowspan
capabilities. Any ideas? Thank you in advance.

This is what I want:
Eric Dog 6
Cat 3
Gerbil 1
Bird 10
Jenny Dog 2
Cat 4

Not this:
Eric Dog 6
Eric Cat 3
Eric Gerbil 1
Eric Bird 10
Jenny Dog 2
Jenny Cat 4
 
A

Aaron Lewis

Ohhhhh, my apologies! I didn't know that your data was joined that way.
In this case, you're absolutely right, there is nothing that the
Repeater has that can intrinsically handle it for ya.

What you *can* do, is in your <ItemTemplate> in the first <TD>, place
something like:

<asp:Label ID=lblUserName Runat=Server></asp:Label>

Then create an event handler of the ItemDataBound event for the
DataRepeater. At the page level, store an arraylist or hashtable or
some other type of collection called something like:

private System.Collections.HashTable shownUsers = new ....;

In the ItemDataBound event, do something like this:

DataRowView view = (DataRowView)e.Item.DataItem;
Label lblUserName = (Label)e.Item.FindControl("lblUserName");
if (shownUsers.ContainsKey(view["UserIDFieldName"].ToString())
{
// Already shown, don't really have to do anything.
}
else
{
// Go ahead and plug in the name field.
lblUserName.Text = view["UserNameFieldName"].ToString();
// Add them to the hashtable of already displayed users.
shownUsers.Add(
new DictionaryEntry(
view["UserIDFieldName"].ToString(),
view["UserNameFieldName"].ToString()
)
);
}

Give that a shot, hope it helps!
 
E

Eric

That did the trick. Many thanks.
I enjoy ASP.net very much but I suppose this is one of those areas
where ASP 3.0 can still be worlds more efficient than ASP.net.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top