views:

247

answers:

1

Hi,

I have a ListView on my checkout page with an ItemTemplate which build up a table of items ordered by customer. I want to add a total in the footer of the table, I have the following markup:

<asp:ListView ID="lvOrderSummary" runat="server">
  <LayoutTemplate>
    <table id="tblOrderSummary">
      <tr>
        <td><b>Title</b></td>
        <td><b>Cost</b></td>
      </tr>
      <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
      <tr>
        <td><b>Total Cost:</b></td>
        <td><%# GetTotalCost().ToString()%></td>
      </tr>
    </table>
  </LayoutTemplate>
  <ItemTemplate>
    <tr>
      <td><%#Eval("Title") %></td>
      <td><%#Eval("Cost") %> </td>
    </tr>
  </ItemTemplate>
</asp:ListView>

I have a server side method called GetTotalCost that return the value I require. The problem I'm having is that this method is never called. I have also tried and instead of using:

<td><%# GetTotalCost().ToString()%></td>

I've tried using

<td id="tdTotal" runat="server"></td>
---------------
protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    TableCell td = ((TableCell)this.FindControl("lvOrderSummary_tdTotal"));
  }
}
+2  A: 

Check this article for an example how to display a total in the ListView.

Basically you can add a label in the layout template:

<asp:ListView ID="lvOrderSummary" runat="server"
  OnPreRender="lvOrderSummary_PreRender" ...>

  <LayoutTemplate>
    ...
    <td><asp:Label ID="lblTotalCost" runat="server" Text="Total"/></td>
    ..
  </LayoutTemplate></asp:ListView>

And then you set the label's text in the PreRender event handler:

protected void lvOrderSummary_PreRender(object sender, EventArgs e)
{
   Label lbl = lvOrderSummary.FindControl("lblTotalCost") as Label;
   lbl.Text = GetTotalCost().ToString();
}
M4N