之前我在PHP中使用过此插件,所以我想我会再次使用它来进行ASP项目。
由于某种原因,它不适用于我的GridView控件。
javascript块:
<link type="text/css" href="../scripts/demo_table.css" rel="stylesheet" />
<script type="text/javascript" language="javascript" src="../scripts/jquery-1.4.1.js"></script>
<script type="text/javascript" language="javascript" src="../scripts/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
$(".gvv").dataTable();
});
</script>
Gridview代码:
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False"
DataKeyNames="Prop_No" DataSourceID="testtt" CssClass="gvv">
我做错了什么或者DataTables不能用于ASP控件吗?
答案 0 :(得分:36)
问题是GridView控件不添加<thead>
元素,只是将标题行放入生成的表的<body>
部分,而数据表插件需要<thead>
部分表。尝试使用以下脚本:
$(function () {
$(".gvv").prepend( $("<thead></thead>").append( $(this).find("tr:first") ) ).dataTable();
});
P.S。您也可以使用不使用Repeater或ListView
等默认布局渲染的控件答案 1 :(得分:15)
您可以使用GridView Prerender事件添加thead
,tbody
和tfoot
代码尝试此代码
protected void GridView1_PreRender(object sender, EventArgs e) {
// You only need the following 2 lines of code if you are not
// using an ObjectDataSource of SqlDataSource
GridView1.DataSource = Sample.GetData();
GridView1.DataBind();
if (GridView1.Rows.Count > 0) {
//This replaces <td> with <th> and adds the scope attribute
GridView1.UseAccessibleHeader = true;
//This will add the <thead> and <tbody> elements
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
//This adds the <tfoot> element.
//Remove if you don't have a footer row
GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
}
}
不要忘记在源页面上添加事件处理程序,如下所示
<asp:GridView ID="GridView1" runat="server" CssClass="gvv"
OnPreRender="GridView1_PreRender">
</asp:GridView>
现在你可以像往常一样简单地调用JQuery函数来渲染它
$(document).ready(function () {
$(".gvv").dataTable();
});
答案 2 :(得分:1)
请尝试以下代码。