按钮单击事件后 DataTable 不呈现网格视图

DataTable not rendring Gridview after button click event

在创建页面时,一切正常,但是当我单击按钮显示所选行时,我的网格视图未呈现为数据表。我需要做什么来解决这个问题或我做错了什么?

我的脚本:

<script type="text/javascript">
        $(function () {
            $('[id*=gvTest]').prepend($("<thead></thead>").append($(this).find("tr:first"))).DataTable({
                "responsive": true,
                "sPaginationType": "full_numbers",
                
                 
            });
        });
        $(document).ready(function () {

            var table = $('#gvTest').DataTable();
            $('#gvTest tbody').on( 'click', 'tr', function () {
            $(this).toggleClass('selected');
            });
              
      
            $('#btnRead').click(function () {
           var ids = $.map(table.rows('.selected').data(), function (item) {
               return item[0]
                 });               
            });
           
    } );
    </script>

我的网格:

<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager>
   <asp:UpdatePanel ID="updatePanel" runat="server">
     <ContentTemplate>

     <asp:GridView  ID="gvTest" Width="100%" runat="server"  AutoGenerateColumns="False" >
      <Columns>
           <asp:BoundField  DataField="PatientID"  HeaderText="Patient ID" >
           </asp:BoundField>
           <asp:BoundField  DataField="PatientName"  HeaderText="PatientName" >
           </asp:BoundField>
           <asp:BoundField  DataField="Age"  HeaderText="Age" >
           </asp:BoundField>
           <asp:BoundField  HeaderText="Sex" DataField="Sex"  >
           </asp:BoundField>
           <asp:BoundField  HeaderText="Mod" DataField="Modality"  >                                 
      </Columns>
    </asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>

我的页面创建时间:

一切正常,但是当我点击一个按钮时会发生这种情况:

我现在需要做什么来解决这个问题?

UpdatePanel 刷新 DOM,因此使用 jQuery 对其所做的任何更改都将丢失。您需要在异步回发后再次调用 DataTable()。您可以为此使用 PageRequestManager

<script type="text/javascript">

    var prm = Sys.WebForms.PageRequestManager.getInstance();

    prm.add_endRequest(function () {
        createDataTable();
    });

    createDataTable();

    function createDataTable() {
        var table = $('#gvTest').DataTable();
        $('#gvTest tbody').on('click', 'tr', function () {
            $(this).toggleClass('selected');
        });
    }
</script>

after using a lots of others code i did'nt get a proper solution so i used this on my page load and its working fine

gvTest.UseAccessibleHeader = true;
//adds <thead> and <tbody> elements
gvTest.HeaderRow.TableSection =
TableRowSection.TableHeader;

解决此问题的最佳方法是在 pageLoad 函数中实现它。 在这种情况下,我使用了 Gridview:

function pageLoad() {
    $("<thead></thead>").append($("#grvPast tr:first")).prependTo($("#grvPast"));
    $('#grvPast').dataTable();
}