How to rid of empty div that contains a GridView

前端 未结 8 759
时光说笑
时光说笑 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:59

    If you're stuck with an unstyled wrapper (which it appears that you are) but want to enforce a style, give it another wrapper, and apply your style to the combination. If a plain div has some padding you want to get rid of (for example), this in the aspx:

    <div id="crushGvDiv">
     <asp:GridView ... >
    </div>
    

    and this for CSS:

    div#crushGvDiv, div#crushGvDiv div { padding: 0; margin: 0; }
    
    0 讨论(0)
  • 2021-01-11 19:00

    Same issue here, OMG it's so annoying. Glitch in rendering in IE6/7 when butting a div to the top of a gridview - the parent DIV causes a space between the two elements.

    I've dug into the GridView code using reflector and found the problem:

    Private Sub Render(ByVal writer As HtmlTextWriter, ByVal renderPanel As Boolean)
        If (Not Me.Page Is Nothing) Then
            Me.Page.VerifyRenderingInServerForm(Me)
        End If
        Me.PrepareControlHierarchy
        If renderPanel Then
            Dim clientID As String = Me.ClientID
            If Me.DetermineRenderClientScript Then
                If (clientID Is Nothing) Then
                    Throw New HttpException(SR.GetString("GridView_MustBeParented"))
                End If
                Dim builder As New StringBuilder("__gv", (clientID.Length + 9))
                builder.Append(clientID)
                builder.Append("__div")
                writer.AddAttribute(HtmlTextWriterAttribute.Id, builder.ToString, True)
            End If
            writer.RenderBeginTag(HtmlTextWriterTag.Div)
        End If
        Me.RenderContents(writer)
        If renderPanel Then
            writer.RenderEndTag
        End If
    End Sub
    

    This is called from render:

    Protected Friend Overrides Sub Render(ByVal writer As HtmlTextWriter)
        Me.Render(writer, Not MyBase.DesignMode)
    End Sub
    

    So, 'renderPanel' == not DesignMode. The DIV is used for paging and sorting when then gridview isn't in an UpdatePanel. On my site, all GridViews are in a UP plus they inherit from a custom gridview class, so my solution was to override the above function with the following:

        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            Me.PrepareControlHierarchy()
            Me.RenderContents(writer)
        End Sub
    

    The other solution could be to copy the render method from above and changed as required.

    This smells of HACK - you've been warned, but might work for you, esp if you're not using paging/sorting.

    0 讨论(0)
提交回复
热议问题