【问题标题】:Populate ListView with data from server使用来自服务器的数据填充 ListView
【发布时间】:2014-01-03 22:26:58
【问题描述】:

我的结帐页面上有一个 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>

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

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

我尝试过使用

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

【问题讨论】:

  • 您的页面加载不完整,因为它除了找到单元格之外什么都不做
  • @John - 抱歉,这只是一个示例,变量 td 返回 null。

标签: asp.net listview


【解决方案1】:

查看this article 了解如何在 ListView 中显示总计的示例。

基本上可以在布局模板中添加标签:

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

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

然后在 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
    

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

    【讨论】:

      猜你喜欢
      • 2011-10-18
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      • 2020-09-12
      相关资源
      最近更新 更多