Solved

ASP.NET Repeater Control + SUM

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

860 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