How to rid of empty div that contains a GridView

前端 未结 8 758
时光说笑
时光说笑 2021-01-11 18:21

In ASP.NET Gridviews generate a table which generates a parent div container. This can break CSS layouts since there is no way to attach styles to the generated div. Is ther

相关标签:
8条回答
  • 2021-01-11 18:34

    UPDATE

    Ians response does remove much of the hackery from this but it also requires more effort on my part. I think we can have the best of both worlds if we do this just a little differently...

    We no longer want to add a 'table-responsive' div to our source at all. We DO want to add a 'table-responsive-table' to our GridView classes.

    ASP

        <asp:GridView ID=gvMain DataSourceID=dsMain RunAt=Server 
                CssClass='table table-responsive-table'>
    

    Our JavaScript just needs to add the 'table-responsive' class to the parent div of those 'table-responsive-table' class tables that we've added.

    JS

    $( document ).ready(function() {
      $(".table-responsive-table").parent().addClass('table-responsive');
    });
    

    This will generate:

    HTML

    <div class=table-responsive>
        <table class='table table-responsive-table' .....>
    

    This new output should be relatively free of hackery related problems due to the fact that we have exactly the same output in the end as we would've otherwise had (except for the extra class on the table), we do not need to modify this code for every table (meaning we can write it once and it'll automatically be applied to all GridViews with the 'table-responsive-table' class), and we are not moving\copying the table data at all (this is important for speed, paging, and sorting). I know everyone says they have the best answer but I really do think this is the absolute best way of handling this.

    NOTE: I have not tested this new code but it will probably work just fine.

    0 讨论(0)
  • 2021-01-11 18:39

    An easy solution without render modifying:

    I need to apply a style to the div generated by the gridview because it breaks my layout, so I created a div with id "myContainerDiv" and moved my GridView into it, and using jQuery I apply some styles.

    Example:

    $("#myContainerDiv > div").css("display", "inline");
    

    I put this javascript in $(document).ready(function({}));. But if you use UpdatePanel, like I must use in this particular case, I execute this $().css() in every async postback. Without that the style will be lost if you execute some updatepanel where your gridview is contained. But I execute this $().css() only if a particular UpdatePanel is fired (no need to execute this javascript instruction in every every every async postback)

    Example:

    <script type="text/javascript">
    
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    
    function EndRequestHandler(sender, args) {
        if (args.get_error() == undefined && sender._updatePanelClientIDs != null &&
                sender._updatePanelClientIDs.length > 0 && sender._updatePanelClientIDs[0] == "<%= MyParticularUpdatePanel.ClientID %>") {
                $("#myContainerDiv > div").css("display", "inline");
            }
        }
    
    </script>
    

    Resolved!

    The entire page will look like that:

    <script type="text/javascrcipt" src="jquery.js"></script>
    
    <script type="text/javascript">    
    
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    
    function EndRequestHandler(sender, args) {
        if (args.get_error() == undefined && sender._updatePanelClientIDs != null &&
                sender._updatePanelClientIDs.length > 0 && sender._updatePanelClientIDs[0] == "<%= MyParticularUpdatePanel.ClientID %>") {
                $("#myContainerDiv > div").css("display", "inline");
            }
        }
    
    </script>
    
    <asp:UpdatePanel runat="server" ID="MyParticularUpdatePanel" UpdateMode="Conditional"  RenderMode="Inline">
    <Triggers>
    // Your triggers here...
    </Triggers>
    <ContentTemplate>
    <div id="myContainerDiv" style="display:inline;">
      <asp:GridView runat="server" ID="MyGridView" AutoGenerateColumns="false" Height="150px"   EmptyDataText="No data.">
         <Columns>
           <asp:BoundField DataField="ID" HeaderText="My ID" />
        </Columns>
      </asp:GridView>
    </div>
    </ContentTemplate>
    </asp:UpdatePanel>
    

    I don't know if this code will compile exactly like I wrote because I wrote it using notepad.

    Sorry for my poor english, I'm from Brazil.

    Christophe Trevisani Chavey. http://www.christophetrevisani.com

    0 讨论(0)
  • 2021-01-11 18:39

    You can put it inside of an asp:Panel and set the Visible property on the panel to false if the table is empty.

    0 讨论(0)
  • 2021-01-11 18:47

    Simplest and best solution using CSS class "gridViewWrapperFix".

    ASPX:

    <div class="gridViewWrapperFix">
    
        <asp:GridView>
        <%--the full gridview would go here--%>
        </asp:GridView>
    
    </div>
    

    CSS:

    /* styles the div that gets auto generated around and asp.net gridview */
    
    .gridViewWrapperFix > div { 
        padding: 0; 
        margin: 0; 
        border: 3px solid red;
    }
    
    0 讨论(0)
  • 2021-01-11 18:55

    I've never done this, but I my first guess would be you could grab the rendered html output just before it gets to the browser, remove the outer div and then htmltextwrite out your new rendered html in the prerender event or make a user or custom control to do this.

    But then you risk breaking the functionality of the gridview but if you know you won't be using the features that use the div, then you might get away with it.

    0 讨论(0)
  • 2021-01-11 18:56

    You could define an explicit CssClass for your Gridviews to make use of.

    <asp:GridView ... CssClass="nameOfStyleClass" ... />
    

    Then define a css class:

    .nameOfStyleClass 
    {
        < Style stuff >
    }
    
    0 讨论(0)
提交回复
热议问题