Solved

ASP.NET Repeater Control + SUM

Posted on 2008-10-17
4
1,486 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

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

713 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