Populating a ListView with data from the server

I have a ListView in my checkout page with an ItemTemplate that creates a table of items ordered by the customer. I want to add the total in the table footer, 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 returns the value I want. The problem I am running into is that this method is never called. I also tried and instead of using:

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

      

I have 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"));
  }
}

      

+1


a source to share


2 answers


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

Basically you can add a label to your 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 text in your PreRender event handler:

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

      

+2


a source


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 processing control is the DetailsView control, which gives you the ability to display, delete, edit, insert one record at a time from its associated data source. The DetailsView control does not support sorting. By default, the DetailsView control displays each record field on its own row.

0


a source







All Articles