CS页面中的数据绑定ListView无法识别aspx页面中的ItemTemplate

时间:2014-08-20 13:52:43

标签: asp.net listview data-binding itemtemplate

我正在尝试将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>

1 个答案:

答案 0 :(得分:1)

在您的代码中,您有以下行:

ListView lvOtherAccts = new ListView();

评论出来。您不需要创建Listview的新实例,因为它已经在您的aspx页面中。