Solved

ASP.NET Repeater Control + SUM

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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