2016-12-08 2 views
1

Lorsque j'utilise jQuery DataTable dans une page ASP.Net normale, cela fonctionne correctement. Cependant, lorsque j'utilise le même même code dans une page maître, il me donne une erreur:jQuery ne fonctionne pas dans la page maître ASP.Net

Uncaught TypeError: $(...).prepend(...).dataTableExt is not a function

Je vous écris deux codes avec des œuvres ou non:

Code de travail:

.aspx code de la page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JqueryDataTables.aspx.cs" Inherits="Portal.WebApp.Login.JqueryDataTables" %> 

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <link href="css/bootstrap.min.css" rel="stylesheet" /> 
    <script src="js/jquery-1.12.3.js"></script> 
    <script src="js/jquery.dataTables.min.js"></script> 
    <link href="css/dataTables.jqueryui.min.css" rel="stylesheet" /> 
    <link href="css/jquery-ui.css" rel="stylesheet" /> 

    <script type="text/javascript"> 
     $(function() { 
      debugger; 
      $("#DataGridView").prepend($("<thead></thead>").append($(this).find("tr:first"))).dataTable(); 
      $('#DataGridView').dataTable(); 
     }); 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <div> 
      <asp:GridView ID="DataGridView" Font-Names="Calibri" Font-Size="11pt" AutoGenerateColumns="false" OnPreRender="DataGridView_PreRender" runat="server"></asp:GridView> 
     </div> 
    </form> 
</body> 
</html> 

.cs code page

using System; 
using System.Data; 
using System.Web.UI.WebControls; 
namespace Citizen.WebApp.Login 
{ 
    public partial class JqueryDataTables : System.Web.UI.Page 
    { 
     RegistrationBLL m_RegistrationBLL = new RegistrationBLL(); 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      DataTable dt = null; 
      dt = m_RegistrationBLL.GetList(); 
      DataGridView.DataSource = dt; 
      int i; 

      for (i = 0; i < dt.Columns.Count; i++) 
      { 
       BoundField test = new BoundField(); 
       test.DataField = dt.Columns[i].ToString(); 
       test.HeaderText = dt.Columns[i].ToString(); 
       DataGridView.Columns.Add(test); 
       test = null; 
      } 
      DataGridView.DataBind(); 
     } 

     protected void DataGridView_PreRender(object sender, EventArgs e) 
     { 
      if (DataGridView.Rows.Count > 0) 
      { 

       DataGridView.UseAccessibleHeader = true; 


       DataGridView.HeaderRow.TableSection = TableRowSection.TableHeader; 


       DataGridView.FooterRow.TableSection = TableRowSection.TableFooter; 
      } 
     } 

    } 
} 

Non Code du travail:

.aspx Page Code du

<%@ Page Title="" Language="C#" MasterPageFile="~/portal/master/Home.Master" AutoEventWireup="true" CodeBehind="MasterDataTable.aspx.cs" Inherits="Portal.WebApp.Login.MasterDataTable" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
    <script src="js/jquery-1.12.3.js"></script> 
    <script src="js/jquery.dataTables.min.js"></script> 
    <link href="css/bootstrap.min.css" rel="stylesheet" /> 
    <link href="css/dataTables.jqueryui.min.css" rel="stylesheet" /> 
    <link href="css/jquery-ui.css" rel="stylesheet" /> 

    <script type="text/javascript" lang="javascript"> 
     $(function() { 
      debugger; 
      $("#DataGridView").prepend($("<thead></thead>").append($(this).find("tr:first"))).dataTableExt(); 
      $('#DataGridView').dataTableExt(); 
     }); 
    </script> 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="cphbody" runat="Server"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head id="Head1"> 
     <title></title> 
    </head> 
    <body> 
     <div> 
      <asp:GridView ID="DataGridView" Font-Names="Calibri" Font-Size="11pt" AutoGenerateColumns="false" OnPreRender="DataGridView_PreRender" runat="server"></asp:GridView> 
     </div> 
    </body> 
    </html> 
</asp:Content> 

.cs page Code

using System; 
using System.Data; 
using System.Web.UI.WebControls; 
namespace Citizen.WebApp.Login 
{ 
    public partial class MasterDataTable : System.Web.UI.Page 
    { 
     RegistrationBLL m_RegistrationBLL = new RegistrationBLL(); 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      DataTable dt = null; 
      dt = m_RegistrationBLL.GetList(); 
      DataGridView.DataSource = dt; 
      int i; 

      for (i = 0; i < dt.Columns.Count; i++) 
      { 
       BoundField test = new BoundField(); 
       test.DataField = dt.Columns[i].ToString(); 
       test.HeaderText = dt.Columns[i].ToString(); 
       DataGridView.Columns.Add(test); 
       test = null; 
      } 
      DataGridView.DataBind(); 
     } 

     protected void DataGridView_PreRender(object sender, EventArgs e) 
     { 
      if (DataGridView.Rows.Count > 0) 
      { 

       DataGridView.UseAccessibleHeader = true; 


       DataGridView.HeaderRow.TableSection = TableRowSection.TableHeader; 


       DataGridView.FooterRow.TableSection = TableRowSection.TableFooter; 
      } 
     } 
    } 
} 
+2

Il est préférable de charger les fichiers de bibliothèque dans MasterPages. Et pourquoi utilisez-vous les balises 'html' et' body' dans les pages enfants? – Satpal

+0

assurez-vous que les chemins sont pertinents pour le nouveau projet. – ScanQR

+0

J'ai fait toutes les solutions possibles sans succès. J'ai vérifié la même chose avec la page de maître vide et obtenant la même réponse sans la chance ... je crois que leurs limites et restriction restreindraient la page principale pour charger et initialiser des datacables de jquery dans les pages principales d'asp.net. –

Répondre

1

appel quelque chose comme ça

$(function() { 
    $("table[id$='DataGridView']").prepend($("<thead></thead>").append($(this).find("tr:first"))).dataTable(); 
}); 

Assurez-vous également que tous les js référencés les fichiers sont présents. Ce qui signifie que vous n'obtenez pas d'erreur 404 dans la console. Deuxièmement, je suggère de garder l'information de rendu de colonne dans le balisage aspx au lieu de le garder dans le code. Comme ci-dessous:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"> 
    <Columns> 
     <asp:BoundField DataField="Id" HeaderText="Customer Id" ItemStyle-Width="90" /> 
     <asp:BoundField DataField="" HeaderText="Name" ItemStyle-Width="120" /> 
     <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100" /> 
    </Columns> 
</asp:GridView> 

N'utilisez pas l'événement prerender, tout cela peut être fait par le balisage. Alors utilisez-le autant que possible. Dans le code, il suffit de lier l'ensemble de données à la vue grille et d'appeler l'événement databind.

GridView1.DataSource = ds; // here assume ds is a dataset 
GridView1.DataBind(); 
+0

Merci mon pote .... son travail bien avec: - $ ("table [id $ = 'DataGridView']"). Prepend ($ ("") .append ($ (this) .find (" tr: premier "))) .dataTable(); J'ai été coincé dans ce depuis 3 derniers jours .. Merci beaucoup pour l'aide .. ☺ –

+0

vous êtes les bienvenus :) –