目前没有加上保持CHECKBOX选择状态,等我有时间在加上,要加JS的,所以就马虎了,很简单的,当然也会有问题,如果发现问题,将来我来改,把问题,写在下面,谢谢,最近没时间改了,我要做一个大的项目,望见谅!
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Drawing
Imports System.Web
Imports System.Web.UI.WebControls
<DefaultProperty("Text"), ToolboxData("<{0}:CheckBoxDataGrid runat=server></{0}:CheckBoxDataGrid>")> _
Public Class CheckBoxDataGrid
Inherits System.Web.UI.WebControls.DataGrid
#Region "プロパティ"
#Region "親クラスから継承された表示プロパティ"
<NotifyParentProperty(True), Browsable(False), _
EditorBrowsable(EditorBrowsableState.Advanced)> _
Public Overrides Property AutoGenerateColumns() As Boolean
Get
Return MyBase.AutoGenerateColumns
End Get
Set(ByVal Value As Boolean)
MyBase.AutoGenerateColumns = False
End Set
End Property
<NotifyParentProperty(True), Browsable(False), _
EditorBrowsable(EditorBrowsableState.Advanced)> _
Public Shadows Property DataMember() As String
Get
Return MyBase.DataMember
End Get
Set(ByVal Value As String)
MyBase.DataMember = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property AllowCustomPaging() As Boolean
Get
Return MyBase.AllowCustomPaging
End Get
Set(ByVal Value As Boolean)
MyBase.AllowCustomPaging = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property AllowPaging() As Boolean
Get
Return MyBase.AllowPaging
End Get
Set(ByVal Value As Boolean)
MyBase.AllowPaging = Value
End Set
End Property
<NotifyParentProperty(True), _
Browsable(False), EditorBrowsable(EditorBrowsableState.Advanced)> _
Public Overrides Property AllowSorting() As Boolean
Get
Return MyBase.AllowSorting
End Get
Set(ByVal Value As Boolean)
MyBase.AllowSorting = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides ReadOnly Property AlternatingItemStyle() As TableItemStyle
Get
Return MyBase.AlternatingItemStyle
End Get
End Property
<NotifyParentProperty(True)> _
Public Overrides Property BackColor() As Color
Get
Return MyBase.BackColor
End Get
Set(ByVal Value As Color)
MyBase.BackColor = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property BackImageUrl() As String
Get
Return MyBase.BackImageUrl
End Get
Set(ByVal Value As String)
MyBase.BackImageUrl = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property BorderColor() As Color
Get
Return MyBase.BorderColor
End Get
Set(ByVal Value As Color)
MyBase.BorderColor = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property BorderStyle() As BorderStyle
Get
Return MyBase.BorderStyle
End Get
Set(ByVal Value As BorderStyle)
MyBase.BorderStyle = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property BorderWidth() As Unit
Get
Return MyBase.BorderWidth
End Get
Set(ByVal Value As Unit)
MyBase.BorderWidth = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property CellPadding() As Integer
Get
Return MyBase.CellPadding
End Get
Set(ByVal Value As Integer)
MyBase.CellPadding = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property CellSpacing() As Integer
Get
Return MyBase.CellSpacing
End Get
Set(ByVal Value As Integer)
MyBase.CellSpacing = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides ReadOnly Property Columns() As DataGridColumnCollection
Get
Return MyBase.Columns
End Get
End Property
<NotifyParentProperty(True)> _
Public Overrides Property EditItemIndex() As Integer
Get
Return MyBase.EditItemIndex
End Get
Set(ByVal Value As Integer)
MyBase.EditItemIndex = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides ReadOnly Property EditItemStyle() As TableItemStyle
Get
Return MyBase.EditItemStyle
End Get
End Property
<NotifyParentProperty(True)> _
Public Overrides Property Enabled() As Boolean
Get
Return MyBase.Enabled
End Get
Set(ByVal Value As Boolean)
MyBase.Enabled = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides ReadOnly Property FooterStyle() As TableItemStyle
Get
Return MyBase.FooterStyle
End Get
End Property
<NotifyParentProperty(True)> _
Public Overrides Property ForeColor() As Color
Get
Return MyBase.ForeColor
End Get
Set(ByVal Value As Color)
MyBase.ForeColor = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property GridLines() As GridLines
Get
Return MyBase.GridLines
End Get
Set(ByVal Value As GridLines)
MyBase.GridLines = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides ReadOnly Property HeaderStyle() As TableItemStyle
Get
Return MyBase.HeaderStyle
End Get
End Property
<NotifyParentProperty(True)> _
Public Overrides Property Height() As Unit
Get
Return MyBase.Height
End Get
Set(ByVal Value As Unit)
MyBase.Height = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property HorizontalAlign() As HorizontalAlign
Get
Return MyBase.HorizontalAlign
End Get
Set(ByVal Value As HorizontalAlign)
MyBase.HorizontalAlign = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides ReadOnly Property ItemStyle() As TableItemStyle
Get
Return MyBase.ItemStyle
End Get
End Property
<NotifyParentProperty(True)> _
Public Overrides ReadOnly Property PagerStyle() As DataGridPagerStyle
Get
Return MyBase.PagerStyle
End Get
End Property
<NotifyParentProperty(True)> _
Public Overrides Property PageSize() As Integer
Get
Return MyBase.PageSize
End Get
Set(ByVal Value As Integer)
MyBase.PageSize = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property ShowFooter() As Boolean
Get
Return MyBase.ShowFooter
End Get
Set(ByVal Value As Boolean)
MyBase.ShowFooter = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property ShowHeader() As Boolean
Get
Return MyBase.ShowHeader
End Get
Set(ByVal Value As Boolean)
MyBase.ShowHeader = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property Visible() As Boolean
Get
Return MyBase.Visible
End Get
Set(ByVal Value As Boolean)
MyBase.Visible = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property Width() As Unit
Get
Return MyBase.Width
End Get
Set(ByVal Value As Unit)
MyBase.Width = Value
End Set
End Property
<NotifyParentProperty(True)> _
Public Overrides Property CssClass() As String
Get
Return MyBase.CssClass
End Get
Set(ByVal Value As String)
MyBase.CssClass = Value
End Set
End Property
#End Region
<Category("モード選択"), DefaultValue(""), _
Description("selectItemlist")> _
Public ReadOnly Property SelectedItemList() As ArrayList
Get
If Not IsDesignMode() Then
Return GetCheckBoxState()
Else
Return New ArrayList
End If
End Get
End Property
<Category("モード選択"), DefaultValue("False"), _
Description("selectItemlist")> _
Public Property HasCheckBox() As Boolean
Get
Return viewstate("HasCheckBox")
End Get
Set(ByVal Value As Boolean)
viewstate("HasCheckBox") = Value
End Set
End Property
#End Region
Private Function IsDesignMode() As Boolean
If (HttpContext.Current Is Nothing) Then
Return True
Else
Return False
End If
End Function
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim tColumn As TemplateColumn
Dim tmpArrayList As Hashtable
If Me.Columns.Count = 0 Then
tColumn = New TemplateColumn
tColumn.HeaderText = "選択"
tColumn.HeaderStyle.Wrap = False
tColumn.HeaderStyle.Width = Unit.Pixel(35)
tColumn.ItemStyle.Wrap = False
tColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center
Me.Columns.AddAt(0, tColumn)
End If
If Me.HasCheckBox = True Then
Me.Columns(0).Visible = True
Else
Me.Columns(0).Visible = False
End If
tmpArrayList = New Hashtable
tmpArrayList.Add("Z-INDEX", Style.Item("Z-INDEX"))
tmpArrayList.Add("LEFT", Style.Item("LEFT"))
tmpArrayList.Add("POSITION", Style.Item("POSITION"))
tmpArrayList.Add("TOP", Style.Item("TOP"))
Me.Style.Remove("Z-INDEX")
Me.Style.Remove("LEFT")
Me.Style.Remove("POSITION")
Me.Style.Remove("TOP")
writer.Write("<div" & " id='" & Me.ID & "_All' style='")
writer.Write("Z-INDEX: " & tmpArrayList("Z-INDEX") & ";")
writer.Write("LEFT: " & tmpArrayList("LEFT") & ";")
writer.Write("POSITION: " & tmpArrayList("POSITION") & ";")
writer.Write("TOP: " & tmpArrayList("TOP") & "' ms_positioning = 'FlowLayout'>")
OutPutLayout(writer)
writer.Write("</div>")
End Sub
Private Sub OutPutLayout(ByVal output As System.Web.UI.HtmlTextWriter)
Dim visiableColumn() As Boolean
ReDim visiableColumn(Me.Columns.Count - 1)
For i As Integer = 0 To Me.Columns.Count - 1
visiableColumn(i) = Me.Columns(i).Visible
Next
output.Write("<table cellSpacing='0' cellPadding='0' border='0'>")
output.Write("<tr>")
Call ShowLeft("", output)
output.Write("</tr>")
For i As Integer = 0 To Me.Columns.Count - 1
Me.Columns.Item(i).Visible = visiableColumn(i)
Next
output.Write("</table>")
End Sub
Private Sub ShowLeft(ByVal width As String, ByVal output As System.Web.UI.HtmlTextWriter)
Dim divStyle As String
output.Write("<td valign='top'>")
divStyle = "<DIV id='" & Me.ID & "_DivLeft' style='OVERFLOW-X: hidden; WIDTH: " & _
width & ";" _
& " BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; " & _
"' ms_positioning = 'GridLayout' > "
output.Write(divStyle)
For i As Integer = 0 To Me.Items.Count - 1
Me.Items(i).Attributes("id") = Me.ID & "TRD" & i
Next
Me.Style("BORDER-STYLE") = "none"
MyBase.Render(output)
output.Write("</DIV>")
output.Write("</td>")
End Sub
Private Sub XYScrollDataGrid_ItemDataBound( _
ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles MyBase.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem _
Or e.Item.ItemType = ListItemType.Item _
Or e.Item.ItemType = ListItemType.EditItem _
Or e.Item.ItemType = ListItemType.SelectedItem Then
If e.Item.ItemIndex <> -1 Then
e.Item.Cells(0).Text = "<input type=checkbox" & _
" name='" & Me.ID & "_checkbox_" & e.Item.ItemIndex & "'>"
End If
End If
End Sub
Private Function GetCheckBoxState() As ArrayList
Dim retArraylist As ArrayList
Dim i As Integer
retArraylist = New ArrayList
For i = 0 To PageSize - 1
If Not HttpContext.Current.Request( _
Me.ID & "_checkbox_" & i) Is Nothing Then
retArraylist.Add(1)
Else
retArraylist.Add(0)
End If
Next
Return retArraylist
End Function
End Class