【问题标题】:Databound ListView in CS page doesn't recognize ItemTemplate in aspx pageCS页面中的数据绑定ListView无法识别aspx页面中的ItemTemplate
【发布时间】: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


    【解决方案1】:

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

    ListView lvOtherAccts = new ListView();
    

    注释掉。您不需要创建 Listview 的新实例,因为它已经在您的 aspx 页面中。

    【讨论】:

    • 太好了,做到了!感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2016-02-20
    相关资源
    最近更新 更多