How to rid of empty div that contains a GridView

前端 未结 8 760
时光说笑
时光说笑 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 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.

提交回复
热议问题