ASP.NET codebehind populated HTML table not getting DataTables treatment

ASP.NET codebehind populated HTML table not getting DataTables treatment

wkhorwkhor Posts: 3Questions: 2Answers: 0
edited October 2015 in Free community support

Hi,
I am adding a literal control into a placeholder in ASP.net to display a HTML table populated from a SQL server database The HTML table is being displayed correctly but other than that, nothing happens, no sorting, etc. just a plain vanilla html table.

Out of frustration, I added a html table and some rows of data into the aspx page and tested it, well, everything works. Now, I am suspecting that the table name inserted via codebehind is not being pick up by the javascript. Would appreciate your comments, I could have missed something simple. Thanks!

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="search.aspx.vb" Inherits="Search2016.search" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>

        <link href="Content/bootstrap.min.css" rel="stylesheet" />
        <link href="Content/bootstrap-theme.min.css" rel="stylesheet" />
        <link href="Content/DataTables/css/jquery.dataTables.min.css" rel="stylesheet" />
        <script src="Scripts/jquery-1.9.1.js"></script>
        <script src="Scripts/bootstrap.min.js"></script>
        <script src="Scripts/DataTables/jquery.dataTables.min.js"></script>

        <link rel="stylesheet" href="font-awesome-4.4.0/css/font-awesome.min.css" />
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />

        <script type="text/javascript">
            $(document).ready(function () {
                $('#example').DataTable();
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:PlaceHolder ID="PH1" runat="server"></asp:PlaceHolder>
        </div>
        </form>
    </body>
    </html>

**** ASPX ****
Imports Search2016.classDB

    Public Class search
        Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                Dim _dTable As New DataTable
                Dim _html As New StringBuilder
                _dTable = getSQLDataTable("PhoneBook", "select * from company")
                _html.Clear()
                With _html
                    .Append("<table id='example' class='table table-striped'")
                    .Append("<thead>")
                    .Append("<tr>")
                    For Each _dCol As DataColumn In _dTable.Columns
                        .Append("<th>")
                        .Append(_dCol.ColumnName)
                        .Append("</th>")
                    Next
                    .Append("</tr>")
                    .Append("</thead>")
                    .Append("<tbody>")
                    For Each _dRow As DataRow In _dTable.Rows
                        .Append("<tr>")
                        For Each _dCol As DataColumn In _dTable.Columns
                            .Append("<td>")
                            .Append(_dRow.Item(_dCol.Ordinal).ToString)
                            .Append("</td>")
                        Next
                        .Append("</tr>")
                    Next
                    .Append("</tbody>")
                    .Append("</table>")
                End With
                Dim _literal As New Literal
                _literal.Text = _html.ToString
                PH1.Controls.Add(_literal)
            End If
        End Sub

    End Class

Answers

This discussion has been closed.