Repeater control footer summary (asp.net 2 with vb)

S

sck10

Hello,

I have a repeater that is bound to a SQL Server table. I would like to
place a summary in the footer for the item count and product cost.

I have two fields. One for the product name and the other for product
cost.

<%#Container.DataItem("ProductA")%>
<%#Container.DataItem("PriceA")%>

My question is, how to count the products (ProductA), get the total price
(sum PriceA) and then place the value in the footer.

Any help with this would be appreciated.
 
S

Steven Cheng[MSFT]

Hi Sck10,

Welcome to ASPNet newsgroup.
From your descrition, you're using the ASP.NET repeater control to display
some records on the page, the datasource contains a price column(decimal?).
So you're wantting to put two labels in the repeater's Footer template so
as to display the Items Count and Total price (sum) of the records in
datasource, yes?

Based on my experience, the most common means to do this is utilizing the
Repeater control's ItemDataBound event. This event will fire during the
databinding of each item in repeater (Header, item/alternateitem/
footer....). So we can just add a <HeaderTemplate> ... into repeater (so
that it will be bound in ItemDataBound event...) and define an member
variable in Page class which can be used to store the Price sum value
during the ItemDataBound event. Then, add this value eachtime the Item or
AlternateItem is bound with data. For example:

"price_sum" is a page's member variable (of decimal type)....
====================
protected void Repeater1_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
price_sum = 0;
}
else if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
price_sum +=
(decimal)((DataRowView)e.Item.DataItem)["UnitPrice"];

}
else if (e.Item.ItemType == ListItemType.Footer)
{
Label lblCount = e.Item.FindControl("lblItemCount") as Label;
lblCount.Text = "Count: " + Repeater1.Items.Count;

Label lblSum = e.Item.FindControl("lblItemPrice") as Label;
lblSum.Text = "Total Price: " + price_sum;
}
}
=====================

=======repeater template========
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"
OnDataBinding="Repeater1_DataBinding"
OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<br />
</HeaderTemplate>
<ItemTemplate >
<br /><%# Eval("ProductName") %>, <%# Eval("UnitPrice","{0:C}") %>
</ItemTemplate>
<FooterTemplate >
<hr size="1" width="100%" />
<asp:Label ID="lblItemCount" runat="server" Text=""></asp:Label>
<asp:Label ID="lblItemPrice" runat="server" Text=""></asp:Label>
</FooterTemplate>
</asp:Repeater>
============================


Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "sck10" <[email protected]>
| Subject: Repeater control footer summary (asp.net 2 with vb)
| Date: Mon, 28 Nov 2005 16:03:42 -0600
| Lines: 23
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 189.202.185.135.in-addr.arpa 135.185.202.189
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:361354
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hello,
|
| I have a repeater that is bound to a SQL Server table. I would like to
| place a summary in the footer for the item count and product cost.
|
| I have two fields. One for the product name and the other for product
| cost.
|
| <%#Container.DataItem("ProductA")%>
| <%#Container.DataItem("PriceA")%>
|
| My question is, how to count the products (ProductA), get the total price
| (sum PriceA) and then place the value in the footer.
|
| Any help with this would be appreciated.
|
| --
| Thanks in advance,
|
| sck10
|
|
|
|
 
E

Eliyahu Goldin

This is a c# ItemDataBound event handler I wrote for a similar task.

protected void dgCoding_ItemDataBound (object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
System.Web.UI.WebControls.Repeater rep = sender as
System.Web.UI.WebControls.Repeater;

if (e.Item.ItemType == ListItemType.Footer)
{
// count totals
decimal totalFee = 0;
foreach (System.Web.UI.WebControls.RepeaterItem item in
rep.Items)
{
System.Web.UI.HtmlControls.HtmlTableRow row =
item.FindControl ("trCod") as System.Web.UI.HtmlControls.HtmlTableRow;
string fee = row.Cells[6].InnerText;
if (fee != String.Empty)
totalFee += System.Convert.ToDecimal (fee);
}
System.Web.UI.HtmlControls.HtmlTableRow totalRow =
e.Item.FindControl ("trTotal") as System.Web.UI.HtmlControls.HtmlTableRow;
totalRow.Cells[1].InnerText = totalFee.ToString ("f2");
}
}

Eliyahu
 
S

sck10

Thanks Steve and Eliyahu.


Eliyahu Goldin said:
This is a c# ItemDataBound event handler I wrote for a similar task.

protected void dgCoding_ItemDataBound (object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
System.Web.UI.WebControls.Repeater rep = sender as
System.Web.UI.WebControls.Repeater;

if (e.Item.ItemType == ListItemType.Footer)
{
// count totals
decimal totalFee = 0;
foreach (System.Web.UI.WebControls.RepeaterItem item in
rep.Items)
{
System.Web.UI.HtmlControls.HtmlTableRow row =
item.FindControl ("trCod") as System.Web.UI.HtmlControls.HtmlTableRow;
string fee = row.Cells[6].InnerText;
if (fee != String.Empty)
totalFee += System.Convert.ToDecimal (fee);
}
System.Web.UI.HtmlControls.HtmlTableRow totalRow =
e.Item.FindControl ("trTotal") as System.Web.UI.HtmlControls.HtmlTableRow;
totalRow.Cells[1].InnerText = totalFee.ToString ("f2");
}
}

Eliyahu

sck10 said:
Hello,

I have a repeater that is bound to a SQL Server table. I would like to
place a summary in the footer for the item count and product cost.

I have two fields. One for the product name and the other for product
cost.

<%#Container.DataItem("ProductA")%>
<%#Container.DataItem("PriceA")%>

My question is, how to count the products (ProductA), get the total price
(sum PriceA) and then place the value in the footer.

Any help with this would be appreciated.
 
S

Steven Cheng[MSFT]

You're welcome Sck10,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "sck10" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Repeater control footer summary (asp.net 2 with vb)
| Date: Tue, 29 Nov 2005 08:56:51 -0600
| Lines: 59
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 189.202.185.135.in-addr.arpa 135.185.202.189
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:361476
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Thanks Steve and Eliyahu.
|
|
| | > This is a c# ItemDataBound event handler I wrote for a similar task.
| >
| > protected void dgCoding_ItemDataBound (object sender,
| > System.Web.UI.WebControls.RepeaterItemEventArgs e)
| > {
| > System.Web.UI.WebControls.Repeater rep = sender as
| > System.Web.UI.WebControls.Repeater;
| >
| > if (e.Item.ItemType == ListItemType.Footer)
| > {
| > // count totals
| > decimal totalFee = 0;
| > foreach (System.Web.UI.WebControls.RepeaterItem item in
| > rep.Items)
| > {
| > System.Web.UI.HtmlControls.HtmlTableRow row =
| > item.FindControl ("trCod") as System.Web.UI.HtmlControls.HtmlTableRow;
| > string fee = row.Cells[6].InnerText;
| > if (fee != String.Empty)
| > totalFee += System.Convert.ToDecimal (fee);
| > }
| > System.Web.UI.HtmlControls.HtmlTableRow totalRow =
| > e.Item.FindControl ("trTotal") as
System.Web.UI.HtmlControls.HtmlTableRow;
| > totalRow.Cells[1].InnerText = totalFee.ToString ("f2");
| > }
| > }
| >
| > Eliyahu
| >
| > | > > Hello,
| > >
| > > I have a repeater that is bound to a SQL Server table. I would like
to
| > > place a summary in the footer for the item count and product cost.
| > >
| > > I have two fields. One for the product name and the other for
product
| > > cost.
| > >
| > > <%#Container.DataItem("ProductA")%>
| > > <%#Container.DataItem("PriceA")%>
| > >
| > > My question is, how to count the products (ProductA), get the total
| price
| > > (sum PriceA) and then place the value in the footer.
| > >
| > > Any help with this would be appreciated.
| > >
| > > --
| > > Thanks in advance,
| > >
| > > sck10
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top