代码之家  ›  专栏  ›  技术社区  ›  ArunPratap Miguel

按钮单击事件后DataTable未呈现Gridview

  •  0
  • ArunPratap Miguel  · 技术社区  · 7 年前

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

    我的脚本:

    <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>
    

    创建我的页面时:

    enter image description here

    一切正常,但当我单击按钮时,会发生以下情况:

    enter image description here

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

    3 回复  |  直到 4 年前
        1
  •  2
  •   VDWWD    7 年前

    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>
    
        2
  •  1
  •   Marcel Gosselin    5 年前

    解决此问题的最佳方法是在 pageLoad 作用 在本例中,我使用了Gridview:

    function pageLoad() {
        $("<thead></thead>").append($("#grvPast tr:first")).prependTo($("#grvPast"));
        $('#grvPast').dataTable();
    }
    
        3
  •  0
  •   ArunPratap Miguel    7 年前

    在使用了很多其他代码之后,我没有得到一个合适的解决方案,所以我在页面加载中使用了它,它工作得很好

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