使用来自服务器的数据填充ListView

时间:2022-09-25 21:37:08

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:

我的结帐页面上有一个ListView,其中包含一个ItemTemplate,它构建了一个客户订购的商品表。我想在表格的页脚中添加一个总数,我有以下标记:

<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:

我有一个名为GetTotalCost的服务器端方法,它返回我需要的值。我遇到的问题是永远不会调用此方法。我也试过而不是使用:

<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 个解决方案

#1


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

查看本文以获取如何在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:

然后在PreRender事件处理程序中设置标签的文本:

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

#2


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.

另一个数据处理控件是DetailsView控件,它使您能够从关联的数据源一次显示,删除,编辑,插入单个记录。 DetailsView控件不支持排序。默认情况下,DetailsView控件在其自己的行上显示记录的每个字段。

#1


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

查看本文以获取如何在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:

然后在PreRender事件处理程序中设置标签的文本:

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

#2


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.

另一个数据处理控件是DetailsView控件,它使您能够从关联的数据源一次显示,删除,编辑,插入单个记录。 DetailsView控件不支持排序。默认情况下,DetailsView控件在其自己的行上显示记录的每个字段。