【发布时间】:2014-08-20 13:52:43
【问题描述】:
我正在尝试将 ListView 绑定到后面代码中的数据表,并使其显示在 aspx 页面上。我得到“必须在 ListView 中定义一个 ItemTemplate ''。”即使 ListView 中存在 ItemTemplate 也会出错。该错误很奇怪,因为它没有在引号中给出我的 Listview 的名称。我没有正确绑定它吗?我不需要其他模板,因为这是一个只读列表。谢谢!
Source Error:
Line 61: lvOtherAccts.DataBind();
CS code:
String strConnString10 = WebConfigurationManager.ConnectionStrings["billing_webConnectionString"].ConnectionString;
SqlConnection con10 = new SqlConnection(strConnString10);
SqlCommand cmd10 = new SqlCommand("SELECT landlord_nbr, svc_addr, svc_addr2, svc_city, svc_state, svc_zip, acct_nbr, billing_date, w_bal from landlord_info where landlord_nbr='" + ll_num + "'ORDER BY acct_nbr ASC", con10);
cmd10.Parameters.Add("conn_nbr", SqlDbType.VarChar).Value = Session["LLNum"];
cmd10.Connection = con10;
SqlDataAdapter da10 = new SqlDataAdapter(cmd10);
DataTable dtLLAccts = new DataTable();
da10.Fill(dtLLAccts);
ListView lvOtherAccts = new ListView();
lvOtherAccts.DataSource = dtLLAccts;
lvOtherAccts.DataBind();
aspx:
<asp:ListView ID="lvOtherAccts" runat="server" DataSourceID="dtLLAccts" ItemPlaceholderID=
"itemPlaceHolder">
<LayoutTemplate>
<table>
<tr>
<th id="Th1" runat="server">
Account
</th>
<th id="Th2" runat="server">
Service Address
</th>
<th id="Th3" runat="server">
City
</th>
<th id="Th4" runat="server">
Last Bill Date
</th>
<th id="Th5" runat="server">
Billed Balance Due
</th>
</tr>
<tr ID="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="AcctNbr" runat="server" Text='<%#Eval("acct_nbr")%>' />
</td>
<td>
<asp:Label ID="SvcAddr" runat="server" Text='<%#Eval("svc_addr")%>' />
</td>
<td>
<asp:Label ID="SvcCity" runat="server" Text='<%#Eval("svc_city")%>' />
</td>
<td>
<asp:Label ID="BillDate" runat="server" Text='<%#Eval("billing_date")%>' />
</td>
<td style="text-align: right;">
<asp:Label ID="Balance" runat="server" Text='<%# Eval("w_bal", "{0:C2}") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
【问题讨论】:
标签: asp.net listview data-binding itemtemplate