2

我已经在这个上搜索了一段时间,我很困惑。对于我正在研究的东西,我有一个非常非常基本的原型,但我不确定出了什么问题。我有一个在代码隐藏中绑定数据的中继器,我在 .js 中为它调用 .datatable ...该表显示了所有花哨的功能,但没有一个功能真正起作用。这是我的代码,我错过了什么吗?

标记:

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Repeater ID="repeater" runat="server">
        <HeaderTemplate>
            <table id="booksTable" border="1" width="100%">
                <thead>
                    <tr>
                        <th>
                            author
                        </th>
                        <th>
                            title
                        </th>
                        <th>
                            genre
                        </th>
                    </tr>
                </thead>
        </HeaderTemplate>
        <ItemTemplate>
            <tbody>
                <tr>
                    <td>
                        <%#DataBinder.Eval(Container.DataItem, "author")%>
                    </td>
                    <td>
                        <%#DataBinder.Eval(Container.DataItem, "title")%>
                    </td>
                    <td>
                        <%#DataBinder.Eval(Container.DataItem, "genre")%>
                    </td>
                </tr>
            </tbody>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    <script type="text/javascript" src="js\jquery-1.4.1.js"></script>
    <script type="text/javascript" src="js\jquery.dataTables.js"></script>
    <script type="text/javascript" src="js\GridControls.js"></script>
</asp:Content>

代码隐藏:

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dataTable = CreateDataTable();
                repeater.DataSource = dataTable;
                repeater.DataBind();
            }
        }

        private static DataTable CreateDataTable()
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add(new DataColumn("Author", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Title", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Genre", typeof(string)));
            dataTable.Rows.Add("douglas Adams", "the hitchhiker's guide to the galaxy", "science fiction");
            dataTable.Rows.Add("j.d. salinger", "the catcher in the rye", "fiction");
            dataTable.Rows.Add("aldous huxley", "brave new world", "science fiction");
            dataTable.Rows.Add("ayn rand", "atlus shrugged", "fiction");
            dataTable.Rows.Add("barbara w. tuchman", "the guns of august", "non-fiction");
            dataTable.Rows.Add("joshua foer", "moonwalking with einstein", "non-fiction");
            dataTable.Rows.Add("esther forbes", "johnny tremain", "historical fiction");
            dataTable.Rows.Add("harold keith", "rifles for watie", "historical fiction");
            dataTable.Rows.Add("tom clancy", "rainbow six", "military fiction");
            dataTable.Rows.Add("tom clancy", "clear and present danger", "military fiction");
            dataTable.Rows.Add("tom clancy", "the sum of all fears", "military fiction");
            dataTable.Rows.Add("stephen king", "christine", "horror fiction");
            dataTable.Rows.Add("stephen king", "pet cemetary", "horror fiction");
            dataTable.Rows.Add("stephen king", "the shining", "horror fiction");
            dataTable.Rows.Add("brian jacques", "redwall", "fantasy fiction");
            dataTable.Rows.Add("benjamin franklin", "the autobiography of benjamin franklin", "autobiography");
            return dataTable;
        }
    }
}

javascript:

$(document).ready(function () {
    $('#booksTable').dataTable({
        'sPaginationType': 'full_numbers'
    });
});
4

2 回答 2

3

当你这样做时:

<ItemTemplate>
    <tbody>
        <tr>
            <td>
                <%#DataBinder.Eval(Container.DataItem, "author")%>
            </td>
            <td>
                <%#DataBinder.Eval(Container.DataItem, "title")%>
            </td>
            <td>
                <%#DataBinder.Eval(Container.DataItem, "genre")%>
            </td>
        </tr>
    </tbody>
</ItemTemplate>

您正在添加多个<tbody>元素。添加<tbody>到你的HeaderTemplate</tbody>你的FooterTemplate,然后再试一次。

于 2013-04-12T18:38:23.617 回答
1
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Repeater ID="repeater" runat="server">
        <HeaderTemplate>
            <table id="booksTable" border="1" width="100%">
                <thead>
                    <tr>
                        <th>
                            author
                        </th>
                        <th>
                            title
                        </th>
                        <th>
                            genre
                        </th>
                    </tr>
                </thead>
         <tbody>
        </HeaderTemplate>
        <ItemTemplate>

                <tr>
                    <td>
                        <%#DataBinder.Eval(Container.DataItem, "author")%>
                    </td>
                    <td>
                        <%#DataBinder.Eval(Container.DataItem, "title")%>
                    </td>
                    <td>
                        <%#DataBinder.Eval(Container.DataItem, "genre")%>
                    </td>
                </tr>

        </ItemTemplate>
        <FooterTemplate>
           </tbody>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    <script type="text/javascript" src="js\jquery-1.4.1.js"></script>
    <script type="text/javascript" src="js\jquery.dataTables.js"></script>
    <script type="text/javascript" src="js\GridControls.js"></script>
</asp:Content>
于 2017-05-29T07:52:01.847 回答