Pod*_*ays 12 c# asp.net jquery jquery-datatables
我以前在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>
Run Code Online (Sandbox Code Playgroud)
Gridview代码:
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False"
DataKeyNames="Prop_No" DataSourceID="testtt" CssClass="gvv">
Run Code Online (Sandbox Code Playgroud)
我做错了什么或DataTables不能用于ASP控件?
Yur*_*kiy 39
问题是GridView控件不添加<thead>
元素,只是将标题行放入<body>
生成的表的一部分,而数据表插件需要表中的一个<thead>
部分.尝试使用以下脚本:
$(function () {
$(".gvv").prepend( $("<thead></thead>").append( $(this).find("tr:first") ) ).dataTable();
});
Run Code Online (Sandbox Code Playgroud)
PS你也可以使用那些不使用Repeater或ListView等默认布局渲染的控件
Pra*_*hav 15
您可以添加thead
,tbody
并tfoot
使用GridView的预渲染事件代码试试这个代码
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;
}
}
Run Code Online (Sandbox Code Playgroud)
不要忘记在源页面上添加事件处理程序,如下所示
<asp:GridView ID="GridView1" runat="server" CssClass="gvv"
OnPreRender="GridView1_PreRender">
</asp:GridView>
Run Code Online (Sandbox Code Playgroud)
现在你可以像往常一样简单地调用JQuery函数来渲染它
$(document).ready(function () {
$(".gvv").dataTable();
});
Run Code Online (Sandbox Code Playgroud)