2013-08-28 153 views
2

我在ASP.NET和UpdatePanel(scriptManager)中使用Jquery Datatable。 我有下以下錯誤:DataTables警告(表ID ='IDTableName'):無法重新初始化DataTable

DataTables warning (table id = 'tbVerificationApplicant'): Cannot reinitialise DataTable.

To retrieve the Datatables object for this table, pass no argument or see the docs for bRetrieve and bDestroy

這是Jquery的文件來創建表:

function DatatablesExec() { 
     $('#tbVerificationApplicant').dataTable({ 
      'bProcessing': true, 
      'bServerSide': true, 
      "sPaginationType": "full_numbers", 
      'sAjaxSource': 'listVerificationData.ashx?ddlStatusValue=' + $("#ddlStatusClient option:selected").text(), 
      "fnDrawCallback": function() { 
       $('#tbVerificationApplicant tbody tr').click(function() { 
        var hRef = $("td:eq(0)", this).text(); 
        document.location.href = 'frm_VerifyIdentity.aspx?ID=' + hRef; 
       }); 
      } 
    }); 
} 

$(document).ready(function() { 
    /* Initialise the DataTable */ 

     DatatablesExec() 

}); 

但是,爲了避免該表desapear後,我改變了下拉列表,我添加的下下面的代碼在網頁表單後面的代碼中。

protected void Page_Prerender(object sender, EventArgs e) 
{ 
    { 
     ScriptManager.RegisterStartupScript(this, this.GetType(), "_function_dummyname", "<script type='text/javascript'>DatatablesExec();</script>", false); 
    } 
} 

它運行良好,但在開始時出現這個錯誤彈出。

氏是網頁形式的一部分:

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <Triggers> 
     <asp:AsyncPostBackTrigger ControlID="ddlStatusClient" EventName="SelectedIndexChanged" /> 
    </Triggers> 
    <ContentTemplate> 
     <table id="tbVerificationApplicant" class="display"> 
       <thead> 

回答

3

當你的頁面加載,你的函數DatatablesExec()被執行兩次:一次是在代碼隱藏Page_Prerender$(document).ready函數一次。

該錯誤表示您試圖使表格成爲Datatable,當它實際上已經是Datatable時。

你將不得不在這裏重新考慮你的設計。

爲什麼需要PreRender中的RegisterStartupScript?

嘗試刪除兩個DatatablesExec()之一,你的問題應該消失!

+0

嗨。我在PreRender中使用了RegisterStartupScript,因爲我使用的是UpdatePanel控件,當我點擊下拉列表時,表desapear。那爲什麼,如果我刪除它,這將不再有效。我需要從PreRender中的RegisterStartupScript引用Jquery中的函數。你知道我怎麼修改它? –

+0

我在函數中添加了「bDestroy」:true,並且正在工作,錯誤desapear,但第一個負載中的佈局正在丟失。你知道爲什麼嗎? –

+0

事實上,它不工作。 「bDestroy」:真的沒用。 –

1

我也有這個問題,我讀了不同的線程解決這個問題,但沒有運氣。 但最後我已經初始化jQuery的窗口load方法

$(window).load(function(){ 
$("#example").DataTable(); 
}); 

,而不是jQuery的文件準備好方法

$(document).ready(function(){ 
$("#example").DataTable(); 
}); 

和奇蹟發生的DataTable .. :) 希望它可以幫助周到,謝謝

1

大家好我只是想在這種情況下添加一些東西,我也體驗它,也搜索這個問題的解決方案,但現在我找到了答案,而不是使用bDestroy總是檢查你的參考ID的數據表在我的情況,如果你已經在使用此

$('#dtAddParticipantList').dataTable 

和您的其他表也用這個

$('#dtAddParticipantList').dataTable 

將其更改爲不同的東西

var oTable = $('#dtParticipantList').dataTable({ 
//some code stuffs 
}); 
4

這驅使我堅果,也因此我想我會添加爲我工作的解決方案。

如果您要發送您的表中的一些元素反應,在表中配置的任何Ajax請求,您需要設置

bRetrieve: true 

SUVASH'S BLOG explains:

There is an another scenario ,say you send more than one ajax request which response will access same table in same template then we will get error also.In this case fnDestroy method doesn’t work properly because you don’t know which response comes first or later.Then you have to set bRetrieve TRUE in data table configuration.That’s it.

0

嘗試添加destory: true到您的dataTable配置:

$('#dataTable').dataTable({ 
    destory: true, 
    ... 
}); 
相關問題