views:

746

answers:

4

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 there a way to prevent the div from generating or a way to apply a style to it?

This was asked and marked as resolved here but MS just said that the div is needed for the Paging and Sorting functionality. Am I to understand that if I want to use the paging and sorting functionality I can't wrap my own div around it and apply a style? Thanks

A: 

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

David Stratton
Thank you but I believe you misread my question. I'm not trying to hide a gridview. I'm trying to rid of the empty div that is generated around the html table(the markup that a gridview generates) as a parent container.
rockstardev
A: 

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 >
}
Denis Sadowski
The problem is I don't want to use a class on a table. Table elements behave differently than div elements and are a pain to use CSS with. I'd rather use divs if I can. Thanks.
rockstardev
+1  A: 

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.

Steve
It sounds like that would work but probably break the sorting/paging features if I removed the div. I guess there is no built in way to apply a style to the div and still have paging and sorting. I may try grabbing the html and adding a CSS class to the div. Thanks.
rockstardev
A: 

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.

JimmyP