views:

260

answers:

2

I have a repeater that currently displays a list of counties and townships within those counties. The county records and township records are treated the same in the repeator, aside from having different RowFormat attributes. Each record also has a unique id and the unit (whether it be a township or county) name to display. There is also a column for townships to reference their parent counties. I utilize the AlternatingItemTemplate option on the repeator to display alternating colors on each township.

I want to be able to display and hide the township rows when the '+' or '-' images are clicked on each row. To add this image to only the county rows I've added an additional column to the bound DataTable which is either empty (for the townships) or contains a tablecell containing the '+' image:

row["Image"] = "<img src='/includes/plus.gif' />";

This is essentially what I'm after:

alt text

I've tried calling a JavaScript function when the user clicks on the '+' or '-' image on the counties, but I'm not really sure how to access all of the child townships from the javascript call to hide them. I also need to switch the countiy's Image field to display the opposite image (i.e. from '+' -> '-' and vise versa).

<TABLE width="100%" style="border: thin solid #A3A3A3;" cellspacing="0px">
   <asp:repeater id="RepeaterUnits" Runat="server">
    <ItemTemplate>
        <tr '<%# (DataBinder.Eval(Container.DataItem, "RowFormat")) %>' >
            <td style="width: 16px" onclick='ClickCollapse(<%# (DataBinder.Eval(Container.DataItem, "UnitID")) %>);'>
                <%# (DataBinder.Eval(Container.DataItem, "Image")) %>
            </td>
            <td onclick='ClickUnit(<%# (DataBinder.Eval(Container.DataItem, "UnitID")) %>);' width="100%">
                <%# (DataBinder.Eval(Container.DataItem, "UnitName")) %>
            </td>
        </tr>
    </ItemTemplate>
    <AlternatingItemTemplate>
        <tr '<%# (DataBinder.Eval(Container.DataItem, "AltRowFormat")) %>'>
            <td style="width: 16px" onclick='ClickCollapse(<%# (DataBinder.Eval(Container.DataItem, "UnitID")) %>);'>
                <%# (DataBinder.Eval(Container.DataItem, "Image")) %>
            </td>
            <td onclick='ClickUnit(<%# (DataBinder.Eval(Container.DataItem, "UnitID")) %>);' width="100%">
                <%# (DataBinder.Eval(Container.DataItem, "UnitName")) %>
            </td>
        </tr>
    </AlternatingItemTemplate>
</asp:repeater>

I thought about potentially using a repeater for each county inside a datagrid to iterate each county's townships, but as of now I'm storing every record in the same datatable, and therefore would have to separate out the counties and towniships into multiple tables. Can anyone suggest a way to better approach this? I'm relatively new to ASP.NET, so I'm not aware of all of the options available to me.

+1  A: 

Can you put the townships inside of a div and then simply show/hide that div?

Alison
The problem with that is the repeater doesn't distingquish between townships and counties, so I have no way of grouping the townships in a single div. Also, the townships have alternating background colors (its hard to tell from the image) which is why I use the alternatingItemTemplate.
Kevin
Is your data stored hierarchically? If so, you would be to use nested repeaters to achieve the desired effect and not have to use multiple tables.
Alison
@Alison Unfortunately it is not stored hierarchically, while the townships are technically "children" of the counties, the data table does not distinguish this. Nevertheless, I'll look in to nested repeaters to see if I can accomplish what I'm after.
Kevin
Alison
Right now all we do (and this is embarrassing; I did not design this) is loop through the list of all units, and if the name contains "county" then we add it to the local DataTable with the county RowFormatting, otherwise it gets added with township RowFormatting. We do order them so in the table all townships come after their corresponding county.
Kevin
This is definitely doable. Imagine you have a repeater that is only outputing the counties. Next inside of the ItemTemplate you have another repeater which sets its datasource using the County name. You place a div inside the ItemTemplate of the inner repeater and then build the township table.If you're using .net 3.5 then this should be a cinch. If it's .net 2.0 then you'll need to write a bit of code in the ItemRepeater ItemDataBound event.I'm thinking off the top of my head so I have no code samples to share here but this should create the layout you're looking for.
Alison
A: 

If you sort your table with two columns as an example by "County, Township" you can effectively loop through it and every time there's a new County you start a new group of Townships.

You can use Repeater's ItemDataBound event in the code behind to do any fancy formatting you need on a per-row basis.

Leon