有没有办法在asp.net中从gridview创建数据表?

时间:2013-07-11 17:20:07

标签: c# jquery asp.net datatables

我想在asp.net中从gridview创建一个数据表。这就是我到目前为止所拥有的。我想使用jquery datatables插件创建一个可以过滤和排序的表。有没有办法从gridview,使用sql数据源执行此操作,或者还有其他我需要做的事情,比如创建一个html。唯一的问题是我需要能够更新表或动态。

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Glossary.aspx.cs" Inherits="Home.Glossary" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title spellcheck="true">Glossary</title>
    <asp:LoginView ID="LoginView2" runat="server"></asp:LoginView>
    </head>
<body>
    <form id="form1" runat="server">

    <div style="margin-left: 720px">

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <asp:LoginView ID="LoginView1" runat="server">
            <AnonymousTemplate>
                <asp:Login ID="Login1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8" BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#333333">
                    <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
                    <LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775" />
                    <TextBoxStyle Font-Size="0.8em" />
                    <TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />
                </asp:Login>
            </AnonymousTemplate>
        </asp:LoginView>

    </div>

        <asp:SqlDataSource ID="TedGlossary" runat="server" ConnectionString="<%$ ConnectionStrings:Glsry_Taylor %>" SelectCommand="SELECT [TermText], [DefNbr], [DefVerNbr], [DefText], [AmplifyingExplanationText], [SeeAlsoText], [AuthoritativeSrcText], [ScopeName], [DomnName], [GovernanceStateName], [LastUpdtTimestamp] FROM [Glossary] ORDER BY [TermText]"></asp:SqlDataSource>

        <asp:GridView ID="GridView1" runat="server"  AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"

            $(document).ready(function() {
              $('#example').dataTable();
                  } );

             DataKeyNames="TermText,DefNbr,DefVerNbr" DataSourceID="TedGlossary" EnableSortingAndPagingCallbacks="True">

            <Columns>
                <asp:BoundField DataField="TermText" HeaderText="Term" ReadOnly="True" SortExpression="TermText" />
                <asp:BoundField DataField="DefNbr" HeaderText="Definition #" ReadOnly="True" SortExpression="DefNbr" />
                <asp:BoundField DataField="DefVerNbr" HeaderText="Definition Vers #" ReadOnly="True" SortExpression="DefVerNbr" />
                <asp:BoundField DataField="DefText" HeaderText="Definition" SortExpression="DefText" />
                <asp:BoundField DataField="AmplifyingExplanationText" HeaderText="Amplifying Explanation" SortExpression="AmplifyingExplanationText" />
                <asp:BoundField DataField="SeeAlsoText" HeaderText="See Also" SortExpression="SeeAlsoText" />
                <asp:BoundField DataField="AuthoritativeSrcText" HeaderText="Authoritative Source" SortExpression="AuthoritativeSrcText" />
                <asp:BoundField DataField="ScopeName" HeaderText="Scope Name" SortExpression="ScopeName" />
                <asp:BoundField DataField="DomnName" HeaderText="Domn Name" SortExpression="DomnName" />
                <asp:BoundField DataField="GovernanceStateName" HeaderText="Governance State" SortExpression="GovernanceStateName" />
                <asp:BoundField DataField="LastUpdtTimestamp" HeaderText="Last Update" SortExpression="LastUpdtTimestamp" />
            </Columns>

           <script src="DataTables-1.9.4/DataTables-1.9.4/media/js/jquery.js"></script>
        <script src="DataTables-1.9.4/DataTables-1.9.4/media/js/jquery.dataTables.min.js"></script>

        </asp:GridView>




    </form>


</body>
</html>

1 个答案:

答案 0 :(得分:1)

您需要首先将页面元素放在正确的位置,然后尝试链接Javascript文件并在正文中调用jQuery函数,这需要在head部分中完成:

<head>
    <script src="DataTables-1.9.4/DataTables-1.9.4/media/js/jquery.js"></script>
    <script src="DataTables-1.9.4/DataTables-1.9.4/media/js/jquery.dataTables.min.js"></script>
    <script>
        $(document).ready(function () {
            $('#GridView1').dataTable();
        });
    </script>
</head>

另请注意,您必须使用GridView的ID初始化DataTable。搜索将自动启用,要启用过滤,您必须使用TemplateFields代替BoundFields,请参阅http://www.dreamincode.net/forums/topic/222381-insert-data-using-gridview-footerrow/以获取如何转换它们的示例。最后,在Code Behind中,您需要强制GridView生成正确的<thead><tbody><tfoot>部分:

    gvPortfolio.UseAccessibleHeader = true;
    gvPortfolio.HeaderRow.TableSection = TableRowSection.TableHeader;
    gvPortfolio.FooterRow.TableSection = TableRowSection.TableFooter;

只需添加<FooterStyle CssClass="FilterCell" />(或某些此类内容)即可为每个页脚单元格添加一个类,然后使用该类连接您的过滤:

$("tbody td.FilterCell").each(function (i) {
    this.innerHTML = fnCreateSelect(oTable.fnGetColumnData(i));
    $('select', this).change(function () {
        oTable.fnFilter($(this).val(), i);
    });
});

您可以在http://datatables.net/release-datatables/examples/api/multi_filter_select.html找到此示例的其余部分。请注意,如上所示,您正在查找GridView生成的<td>元素,而不是示例所期望的<th>元素。