[GridView]客製化GridView的Caption
這概念其實也沒什麼,就是把GridView的Caption屬性,塞HTML跟CSS。
以我們常用的用法來說,GridView的Caption(基本上他們會講Table的Title)擺放的位置有三種,置左、置中、置右。
而且是會同時發生,也就是左、中、右可能都各自有值。左邊可能擺放整個頁面的段落,中間可能是GridView的表格說明,右邊可能是GridView的單位。
這時候原生的Caption屬性就會有點礙手礙腳。
Caption屬性Render到HTML上,是<caption></caption>,當你塞第二個<caption>他就會在不同列呈現。
所以最簡單的方法,還是塞一個table的HTML進去。
示範一下囉唆的作法,建立3個屬性,分別是置左、置中、置右的Text,再建立3個屬性,是三格的CSS。
Private _strCaptionLeft As String = String.Empty
Private _strCaptionCenter As String = String.Empty
Private _strCaptionRight As String = String.Empty
Private _strCssCaptionLeft As String = String.Empty
Private _strCssCaptionCenter As String = String.Empty
Private _strCssCaptionRight As String = String.Empty
<Category("Caption")> _
Public Property CssCaptionLeft() As String
Get
Return _strCssCaptionLeft
End Get
Set(ByVal value As String)
_strCssCaptionLeft = value
End Set
End Property
<Category("Caption")> _
Public Property CssCaptionCenter() As String
Get
Return _strCssCaptionCenter
End Get
Set(ByVal value As String)
_strCssCaptionCenter = value
End Set
End Property
<Category("Caption")> _
Public Property CssCaptionRight() As String
Get
Return _strCssCaptionRight
End Get
Set(ByVal value As String)
_strCssCaptionRight = value
End Set
End Property
<Category("Caption")> _
Public Property CaptionLeft() As String
Get
Return _strCaptionLeft
End Get
Set(ByVal value As String)
_strCaptionLeft = value
End Set
End Property
<Category("Caption")> _
Public Property CaptionCenter() As String
Get
Return _strCaptionCenter
End Get
Set(ByVal value As String)
_strCaptionCenter = value
End Set
End Property
<Category("Caption")> _
Public Property CaptionRight() As String
Get
Return _strCaptionRight
End Get
Set(ByVal value As String)
_strCaptionRight = value
End Set
End Property
接著在GridView的OnPreRnder()裡,去覆寫Grid的Caption屬性即可。
倘若不想影響到舊有的caption屬性,可以額外加個判斷避掉。
Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
MyBase.OnPreRender(e)
If Me.Caption = String.Empty Then
'25% 50% 25%的td,可由外面設定CSS取代。
'Dim strAllCaption As String = "<table width=""100%""><tr><td align=""left"" style=""width:25%;"" class=""" & Me.CssCaptionLeft & """>" & Me.CaptionLeft & "</td><td align=""center"" style=""width:50%;"" class=""" & Me.CssCaptionCenter & """>" & Me.CaptionCenter & "</td><td align=""right"" style=""width:25%;"" class=""" & Me.CssCaptionRight & """>" & Me.CaptionRight & "</td></tr></table>"
Dim strAllCaption As String = "<table width=""100%""><tr><td align=""left"" class=""" & Me.CssCaptionLeft & """>" & Me.CaptionLeft & "</td><td align=""center"" class=""" & Me.CssCaptionCenter & """>" & Me.CaptionCenter & "</td><td align=""right"" class=""" & Me.CssCaptionRight & """>" & Me.CaptionRight & "</td></tr></table>"
Me.Caption = strAllCaption
End If
End Sub
blog 與課程更新內容,請前往新站位置:http://tdd.best/