1
votes

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
your page load is incomplete as it does nothing other than find the cell - Johnno Nolan
@John - sorry, this is just a sample, the variable td returns null. - Fermin

2 Answers

2
votes

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();
}
0
votes
Try

        Dim strcon As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=D:\webarticles\App_Data\mydatabase.mdf;Integrated Security=True;User Instance=True"
        Dim con As New SqlConnection(strcon)
        con.Open()
        Dim da As SqlDataAdapter
        Dim ds As New DataSet
        Dim sqlstring As String = "SELECT * FROM tblstudent "
        da = New SqlDataAdapter(sqlstring, con)
        da.Fill(ds)
        DetailsView1.DataSource = ds.Tables(0)
        DetailsView1.DataBind()

    Catch ex As Exception
        MsgBox("There is some Error")

    End Try

Another data handeling control is DetailsView control which gives you the ability to display, delete, edit, insert a single record at a time from its associated data source. The DetailsView control does not support sorting.By default, the DetailsView control displays each field of a record on its own line.