Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.NET Repeater Control + SUM

Posted on 2008-10-17
4
Medium Priority
?
1,520 Views
Last Modified: 2012-08-13
Quick summary.  

In my sql statement I am returning 8 values.
      
      ID
      Name
      Location
      RecordDate
      ReviewDate
      ReviewerName
      NumDates
      TotalDays
      

sql = "Select ID, Name, Location, RecordDate, ReviewDate, ReviewerName,  DATEDIFF(DAY, RecordDate, ReviewDate) AS NumDays,  "
sql = sql & "  SUM(DATEDIFF(DAY, RecordDate, ReviewDate)) AS TotalDays  from tblVendor  "

NumDays will equal the difference in days between the RecordDate and the ReviewDate.  
The TotalDays should SUM all the NumDays in the query.


Using the repeater control how do I display the SUM TotalDays in the footer ?  From the sample below everything works fine but when using Container.DataItem, "TotalDays" in the footer it does not display anything.

      <asp:Repeater ID="Repeater1" runat="server" >
        <HeaderTemplate>
        <table border = "0" cellpadding="3" cellspacing="1" >
        <tr>
        <th>ID</th>
        <th>Name</th>    
        <th>Location</th>
        <th>Date Received</th>
        <th>Reviewed By</th>
        <th>Date Reviewed</th>
        <th>Days Diff</th>  
                 
        </tr>
        </HeaderTemplate>
       
        <ItemTemplate>
        <tr>
        <td><%#DataBinder.Eval(Container.DataItem, "ID")%></a></td>
        <td><%#DataBinder.Eval(Container.DataItem, "Name")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "Location")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "RecordDate", "{0:d}")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "ReviewerName")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "ReviewDate", "{0:d}")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "NumDays", "{0:d}")%></td>    
        </tr>            
        </ItemTemplate>
        <FooterTemplate>        
        <tr>
        <td colspan="6">  
        </td>
        <td>[TotalDays should be displayed here] </td>
        </tr>  
        </table>
        </FooterTemplate>              
        </asp:Repeater>
0
Comment
Question by:pgooseman
2 Comments
 
LVL 3

Accepted Solution

by:
3abqari earned 1000 total points
ID: 22742150
Try playing around with this code:

http://www.experts-exchange.com/Web_Development/WebApplications/Q_23762786.html#a22570664

but instead of e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item

try  e.Item.ItemType = ListItemType.FooterItem
0
 
LVL 6

Assisted Solution

by:esolve
esolve earned 1000 total points
ID: 22757632
You have a bit of a weird scenario. I will do the following:
1. Put a label in the Footer
 <FooterTemplate>
             <asp:Label id="lblFooter" text="" runat="server" />
 </FooterTemplate>

2. Then retrieve and assign the label in the repeaters databound event
string sLocalValueToShowInFooter = "";

    protected void Page_Load(object sender, EventArgs e){}

    protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
      DataRowView drv = (DataRowView)e.Item.DataItem;
      if (drv != null)
      {
        sLocalValueToShowInFooter = drv["DataColumn3"].ToString();
      }

      if (e.Item.ItemType == ListItemType.Footer)
      {
        Label lbl = (Label)e.Item.FindControl("lblFooter");
        if (sLocalValueToShowInFooter != null)
        {
          lbl.Text = sLocalValueToShowInFooter;
        }
      }
    }

3. Resoning behind this is:
What row's value should be displayed in the footer? 1st,2nd last.
Also the local variable should probably be saved in viewstate in order not to loose value between postbacks.
So in this example I am retrieving a value to assign to the label control.

Sorry not the nicest example but it works.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Screencast - Getting to Know the Pipeline

877 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