自訂GridView的凍結功能-Part I

自訂GridView的凍結功能-Part I

我在前面有發表了一篇撰寫自訂的GridView TemplateField,文中提到我必須要動態的產生這些欄位

也因此,如果我的課程很多的話,畫面就會被拉寬,醬子就有點不Friendly了

我轉而去思考讓GridView有Scrollbar的功能,有了這功能後,是有比較改善了.......

但大家都知道,人的欲望是無窮無盡的嘛,這時我思考能否做到凍結窗格的功能

 

上網找了幾篇文章後,總算有點頭絮了,這邊分享給大家

首先我新增一個Windows Forms Control Library專案,命名為FrozenGv

將原本的UserControl刪掉,新增一個Class,命名為ServerControl

 

這邊我先講我的方法,程式細項就讓各位去看了

我繼續了GridView,然後新增3個CSS的功能,分別控制水平、垂直及全部的滑動

之後再去覆寫GridView的OnPreRender及Render事件,就可以了,詳細程式碼如下

 

Imports System.Drawing
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web
Imports System.Globalization

Namespace GridViewControl
    <ToolboxData("<{0}:GridView runat=server></{0}:GridView>")> _
Public Class GridView
        Inherits System.Web.UI.WebControls.GridView

#Region "Define CSS"
        Public Const FrozenVerticalCssClass As String = "frozenVertical"
        Public Const FrozenHorizontalCssClass As String = "frozenHorizontal"
        Public Const FrozenAllCssClass As String = "frozenAll"
#End Region

#Region "Define Property"

        <Browsable(True), DefaultValue(ScrollBars.None), Category("Behavior")> _
        Public Property Scrolling() As ScrollBars
            Get
                Dim val As Object = ViewState("Scrolling")
                If (Nothing = val) Then
                    Return ScrollBars.None
                End If

                Return DirectCast(val, ScrollBars)
            End Get
            Set(ByVal value As ScrollBars)
                ViewState("Scrolling") = value
            End Set
        End Property

        Public Property ScrollWidth() As Unit
            Get
                Return MyBase.Width
            End Get
            Set(ByVal value As Unit)
                MyBase.Width = value
            End Set
        End Property

        Public Overrides Property Width() As System.Web.UI.WebControls.Unit
            Get
                Dim val As Object = ViewState("DivWidth")
                If (Nothing = val) Then
                    Return Unit.Empty
                End If

                Return DirectCast(val, Unit)
            End Get
            Set(ByVal value As System.Web.UI.WebControls.Unit)
                ViewState("DivWidth") = value
            End Set
        End Property

        Public Property ScrollHeight() As Unit
            Get
                Return MyBase.Height
            End Get
            Set(ByVal value As Unit)
                MyBase.Height = value
            End Set
        End Property

        Public Overrides Property Height() As System.Web.UI.WebControls.Unit
            Get
                Dim val As Object = ViewState("DivHeight")
                If (Nothing = val) Then
                    Return Unit.Empty
                End If

                Return DirectCast(val, Unit)
            End Get
            Set(ByVal value As System.Web.UI.WebControls.Unit)
                ViewState("DivHeight") = value
            End Set
        End Property

        Private ReadOnly Property OverflowX() As String
            Get
                If (Me.Scrolling = ScrollBars.Horizontal Or Me.Scrolling = ScrollBars.Both) Then
                    Return "scroll"
                ElseIf Me.Scrolling = ScrollBars.Auto Then
                    Return "auto"
                Else
                    Return "visible"
                End If
            End Get

        End Property

        Private ReadOnly Property OverflowY() As String
            Get
                If (Me.Scrolling = ScrollBars.Vertical Or Me.Scrolling = ScrollBars.Both) Then
                    Return "scroll"
                ElseIf Me.Scrolling = ScrollBars.Auto Then
                    Return "auto"
                Else
                    Return "visible"
                End If
            End Get
        End Property

#End Region

#Region "Overrides"
        Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
            MyBase.OnPreRender(e)

            Dim frozenVerticalStyle As New FrozenVerticalStyle()
            Dim frozenHorizontalStyle As New FrozenHorizontalStyle()
            Dim frozenAllStyle As New FrozenAllStyle()

            Me.Page.Items(GridView.FrozenHorizontalCssClass) = "1"
            Me.Page.Header.StyleSheet.CreateStyleRule(frozenHorizontalStyle, Nothing, "." + GridView.FrozenHorizontalCssClass)

            Me.Page.Items(GridView.FrozenVerticalCssClass) = "2"
            Me.Page.Header.StyleSheet.CreateStyleRule(frozenVerticalStyle, Nothing, "." + GridView.FrozenVerticalCssClass)

            Me.Page.Items(GridView.FrozenAllCssClass) = "3"
            Me.Page.Header.StyleSheet.CreateStyleRule(frozenAllStyle, Nothing, "." + GridView.FrozenAllCssClass)

        End Sub

        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

            If (Me.Page IsNot Nothing) Then
                Me.Page.VerifyRenderingInServerForm(Me)
            End If

            Me.PrepareControlHierarchy()

            If Not (Me.DesignMode) Then
                Dim clientID As String = Me.ClientID

                If (clientID Is Nothing) Then
                    Throw New HttpException("FrozenGridView Must Be Parented")
                End If

                writer.AddAttribute(HtmlTextWriterAttribute.Id, String.Format(CultureInfo.InvariantCulture, "__gv{0}__div", clientID), True)
                writer.AddStyleAttribute(HtmlTextWriterStyle.OverflowX, Me.OverflowX)
                writer.AddStyleAttribute(HtmlTextWriterStyle.OverflowY, Me.OverflowY)

                If Not (Me.Width.IsEmpty) Then
                    writer.AddStyleAttribute(HtmlTextWriterStyle.Width, Me.Width.ToString(CultureInfo.InvariantCulture))
                End If

                If Not (Me.Height.IsEmpty) Then
                    writer.AddStyleAttribute(HtmlTextWriterStyle.Height, Me.Height.ToString(CultureInfo.InvariantCulture))
                End If

                writer.AddStyleAttribute(HtmlTextWriterStyle.Position, "relative")
                writer.RenderBeginTag(HtmlTextWriterTag.Div)

            End If

            Me.RenderContents(writer)

            If Not (Me.DesignMode) Then
                writer.RenderEndTag()
            End If

        End Sub

#End Region

        Private Class FrozenVerticalStyle
            Inherits Style

            Protected Overrides Sub FillStyleAttributes(ByVal attributes As System.Web.UI.CssStyleCollection, ByVal urlResolver As System.Web.UI.IUrlResolutionService)
                MyBase.FillStyleAttributes(attributes, urlResolver)

                attributes(HtmlTextWriterStyle.Top) = "expression(this.offsetParent.scrollTop)"
                attributes(HtmlTextWriterStyle.BackgroundColor) = "#ffeaff"
                attributes(HtmlTextWriterStyle.Position) = "relative"
                attributes(HtmlTextWriterStyle.ZIndex) = "20"
            End Sub
        End Class

        Private Class FrozenHorizontalStyle
            Inherits Style

            Protected Overrides Sub FillStyleAttributes(ByVal attributes As System.Web.UI.CssStyleCollection, ByVal urlResolver As System.Web.UI.IUrlResolutionService)
                MyBase.FillStyleAttributes(attributes, urlResolver)

                attributes(HtmlTextWriterStyle.Left) = "expression(this.offsetParent.scrollLeft)"
                attributes(HtmlTextWriterStyle.BackgroundColor) = "#ffeaff"
                attributes(HtmlTextWriterStyle.Position) = "relative"
                attributes(HtmlTextWriterStyle.ZIndex) = "20"
            End Sub
        End Class

        Private Class FrozenAllStyle
            Inherits Style

            Protected Overrides Sub FillStyleAttributes(ByVal attributes As System.Web.UI.CssStyleCollection, ByVal urlResolver As System.Web.UI.IUrlResolutionService)
                MyBase.FillStyleAttributes(attributes, urlResolver)

                attributes(HtmlTextWriterStyle.Top) = "expression(this.offsetParent.scrollTop)"
                attributes(HtmlTextWriterStyle.Left) = "expression(this.offsetParent.scrollLeft)"
                attributes(HtmlTextWriterStyle.BackgroundColor) = "#ffeaff"
                attributes(HtmlTextWriterStyle.Position) = "relative"
                attributes(HtmlTextWriterStyle.ZIndex) = "30"
            End Sub
        End Class

    End Class
End Namespace