?
Solved

Comparing data in a repeater

Posted on 2010-11-18
4
Medium Priority
?
622 Views
Last Modified: 2012-05-10
I am iterating through a repeater to show college courses.  Each college course may or may not have a category / subcategory, and I am showing/hiding the category/subcategory row based on if there is data found in the field or not.

That seems to be working just fine, but now what I need to do is compare category/subcategory fields to the previous record's category/subcategory to detemine if I should show it or not.

Here's what is showing now:

Category 1
>> Subcategory 1
>> >> Course 1
Category 1
>> Subcategory 1
>> >> Course 2
Category 1
>> Subcategory 1
>> >> Course 3
Category 2
>> Subcategory 1
>> >> Course 1
Category 2
>> Subcategory 2
>> >> Course 1

What I want to show is this:

Category 1
>> Subcategory 1
>> >> Course 1
>> >> Course 2
>> >> Course 3
Category 2
>> Subcategory 1
>> >> Course 1
>> Subcategory 2
>> >> Course 1

In order to do this, I need to compare the category and subcategory fields to the record just before it to determine if it is the same or not, but I don't know how to do that in a repeater. Help?
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
           {
                string category = DataBinder.Eval(e.Item.DataItem, "category").ToString();
                string subcat = DataBinder.Eval(e.Item.DataItem, "subcategory").ToString();

                if ((category == "") || (category == "0"))
                {
                    ((Literal)e.Item.FindControl("litCategory")).Visible = false;
                }
                else
                {
                    ((Literal)e.Item.FindControl("litCategory")).Visible = true;
                    ((Literal)e.Item.FindControl("litCategory")).Text = "<tr><td colspan='4' align='left'><h4>" +
                        category + "</h4></td></tr>";
                }
               
               if ((subcat == "") || (subcat == "0"))
                {
                   ((Literal)e.Item.FindControl("litSubcategory")).Visible = false;
                }
                else
                {
                    ((Literal)e.Item.FindControl("litSubcategory")).Visible = true;
                    ((Literal)e.Item.FindControl("litSubcategory")).Text = "<tr><td width='5%'>&nbsp;</td><td colspan='3' align='left'><h3><i>" +
                       subcat + "</i></h3></td></tr>";
                }
}
 
        }

Open in new window

<asp:SqlDataSource ID="dsCourses" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ruUtility %>" 
            SelectCommand="sp_courseRecords" SelectCommandType="StoredProcedure">     
        </asp:SqlDataSource>
        
        <asp:Repeater ID="courseDetail" runat="server" DataSourceID="dsCourses" OnItemDataBound="R2_ItemDataBound">
        <HeaderTemplate><table width="95%" cellpadding="3" cellspacing="0" border="0" align="center"></HeaderTemplate>
        <ItemTemplate>
        <asp:Literal ID="litCategory" runat="server"></asp:Literal>
        <asp:Literal ID="litSubcategory" runat="server"></asp:Literal>
        <tr>
        <td width="5%">&nbsp;</td>
        <td align="left" valign="top" width="15%"><asp:Hyperlink ID="hlCourse" runat="server"><%# Eval("subjectID") %>&nbsp;<%# Eval("courseNumber") %>
            </asp:Hyperlink></td>
        <td align="left" valign="top" width="70%">
            <asp:Hyperlink ID="hlCourseName" runat="server"><%# Eval("courseName") %></asp:Hyperlink></td>
        <td align="left" valign="top" width="10%" nowrap="nowrap">.....<asp:Label ID="lblCredits" runat="server"
            Text='<%# Eval("credits") %>'></asp:Label></td>
        </ItemTemplate>
        <FooterTemplate></table></FooterTemplate>
        </asp:Repeater>

Open in new window

0
Comment
Question by:vcbertini
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:Richard Lee
ID: 34165611
Simple but this might work on entry to your OnDataBound method save the current item in a class field and then you can compare the next item to this one.

DaTribe
0
 

Author Comment

by:vcbertini
ID: 34167070
Hm, I'm new to C# (and writing new classes) - can you give me an example?
0
 
LVL 18

Accepted Solution

by:
Richard Lee earned 1500 total points
ID: 34167469
Something like this:

DaTribe
public class Default : System.Web.UI.Page
{
    // Declare variable here to store previous item
    private MyItem PreviousItem { get; set; }

    public void R2_ItemDataBound(...)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if(PreviousItem != null)
            {
                // Then we have passed the first item
                // Do something
            }
            ...

            PreviousItem = e.Item.DataItem;
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:vcbertini
ID: 34242480
I think I'm going to try Session Variables first. Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question