Solved

ASP.NET Repeater Control + SUM

Posted on 2008-10-17
4
1,472 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
4 Comments
 
LVL 3

Accepted Solution

by:
3abqari earned 250 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 250 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now