c# - 将 jQuery 数据表绑定(bind)到 gridview 时出现异常

标签 c# jquery asp.net gridview datatables

我正在尝试将 jquery 数据表绑定(bind)到 gridview。它抛出异常。

“表格必须按页眉、正文和页脚的顺序包含行部分。”

步骤

1) JavaScript调用

<script type="text/javascript" charset="utf-8">
/* Define two custom functions (asc and desc) for string sorting */
  $(document).ready(function () {
    /* Build the DataTable with third column using our custom sort functions */
    $('#gvInitiavtives').dataTable({
        "aaSorting": [[0, 'asc']          
    });
});

2) GridView

 <asp:GridView ID="gvInitiavtives" runat="server" Width="100%" CssClass="Grid" 
                     RowStyle-Width="30px" AutoGenerateColumns="false" HeaderStyle-CssClass="GridHeader"  RowStyle-CssClass="GridItem" AlternatingRowStyle-CssClass="GridAltItem"  DataKeyNames="InitiativeIdx" AllowSorting="true" ClientIDMode="Static" >
                <EmptyDataRowStyle BorderStyle="None" BorderWidth="0px" />
                <EmptyDataTemplate>
                    <asp:Label ID="lblNorecId" runat="server" Text="Sorry! Your requested records are not found, please try with other search criteria." Font-Bold="true" ForeColor="red" Font-Names="Arial" Font-Size="small"></asp:Label>
                </EmptyDataTemplate>
                <Columns>
                    <asp:BoundField DataField="BusinessUnit" HeaderText="" HeaderStyle-Wrap="false" HeaderStyle-HorizontalAlign="NotSet" SortExpression="BusinessUnit" />
                    <asp:TemplateField>
                    <ItemTemplate>

                    <asp:Label ID="lblInitiaiveName" runat="server" Text='<%# Bind("InitiativeName") %>' ToolTip ='<%# String.Format("{0} ; {1}",Eval("Initiative"), Eval("InitiativeDescription")) %>' ></asp:Label>
                    </ItemTemplate>
                    </asp:TemplateField>
---
---
</Columns>
            </asp:GridView>

3) 代码隐藏

protected void Page_PreRender(object sender, System.EventArgs e)
{
    // support for jquery datatables
    if (gvInitiavtives.Rows.Count > 0) {
        gvInitiavtives.UseAccessibleHeader = true;
        gvInitiavtives.HeaderRow.TableSection = TableRowSection.TableHeader;
    }

}

4) 异常

Server Error in '/' Application.
--------------------------------------------------------------------------------

The table  must contain row sections in order of header, body, then footer. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: The table  must contain row sections in order of header, body, then footer.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[HttpException (0x80004005): The table  must contain row sections in order of header, body, then footer.]
   System.Web.UI.WebControls.Table.RenderContents(HtmlTextWriter writer) +8790317
   System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +32

最佳答案

这样修改代码

$('[id*=gvInitiavtives]').prepend($("<thead></thead>").append($(this).find("[id*=gvInitiavtives] tr:first"))).dataTable({
"aaSorting": [[0, 'asc']          
    });

关于c# - 将 jQuery 数据表绑定(bind)到 gridview 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15756074/

相关文章:

c# - SQLite 数据库插入错误约束

c# - 请求的集合成员不存在,MS Word

javascript - 使用 'this' 单击页面时 jquery 获取元素 id 不起作用

javascript - 表中的 jQuery slideDown 和 SlideUp 看起来不正确

html - 如何解决html中固定列的无限垂直滚动问题

c# - 使用 linq 选择不同的

c# - 仅打印小数值

jquery - MVC 4 Razor 和 Jquery UI datepicker() "Object doesn' t 支持属性或方法”

c# - 如果在 FormView 中,在哪里设置 ListBox Selected 索引?

c# - 使用许多数据表过滤整个数据集