代码之家  ›  专栏  ›  技术社区  ›  Brian Schmitt

“网格视图”复选框列

  •  1
  • Brian Schmitt  · 技术社区  · 16 年前

    我曾经在1.1中为从DataGridColumn类继承的DataGrid设置了一个类。这允许我创建一个复选框列,在头中有一个客户端的un/check all框。然后,在设计网格时,我只需添加自定义列。

    我目前在一个项目中,我需要网格视图的类似功能,但是,似乎没有一种方法来继承或向列添加功能。

    所以我的问题是,是否有方法重写列?或者这个代码已经存在了,以一种可重用的方式?

    需求很简单:我希望它只在页面上注册javascript并呈现一列复选框。

    我已经遇到了4Guys示例,但他们只是将所有代码都放到了代码中,我正在寻找一些少一些复制/粘贴的东西。

    3 回复  |  直到 15 年前
        1
  •  1
  •   devio    16 年前

    我从System.Web.UI.WebControls.BoundField和.HyperlinkField派生类 您可能对从checkboxfield类继承感兴趣 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxfield.aspx

        2
  •  0
  •   AndyG    16 年前

    你可以只使用一个templateColumn和一个itemtemplate,在你的DataGrid列中包含你的复选框吗?

    类似:

    <asp:DataGrid id="DG1" runat = "server" DataKeyField = "ID">
    <Columns>
    <asp:TemplateColumn HeaderText="ProductName">
    <ItemTemplate>
    <asp:CheckBox id="chkBox1" runat="server" 
    Text =<%# DataBinder.Eval(Container.DataItem,"yourDataToBind") %>
    checked='<%# DataBinder.Eval(Container.DataItem,"yourBoolToBind")  %>'>
    </asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    
        3
  •  0
  •   Brian Schmitt    16 年前

    我继承了边界,然后想到了这个:

    页代码:

    <%@ register tagprefix="CAC" namespace="UI.Controls" assembly="UI.Controls" %>    
    <asp:gridview id="grdPrint" runat="server" autogeneratecolumns="False">
        <columns>
            <cac:checkallcolumn />
            <asp:boundfield datafield="CompanyName" headertext="Company Name" />
        </columns>
    </asp:gridview>
    

    这就是控制:

    Imports system.Web.UI
    Imports system.Web.UI.WebControls
    
    Public Class CheckAllColumn
        Inherits BoundField
    
        Public Sub New()
            MyBase.New()
        End Sub
    
        Public ReadOnly Property SelectedIndexes() As List(Of Int32)
            Get
                Dim selectedIndexList As New List(Of Int32)
                Dim grdParent As GridView = CType(Me.Control, GridView)
                For Each item As GridViewRow In grdParent.Rows
                    Dim chkBox As CheckBox = CType(item.FindControl("checkboxCol"), CheckBox)
                    If ((Not (chkBox) Is Nothing) _
                                AndAlso chkBox.Checked) Then
                        selectedIndexList.Add(item.DataItemIndex)
                    End If
                Next
                Return selectedIndexList
            End Get
        End Property
    
        Public ReadOnly Property SelectedDataKeys() As Object()
            Get
                Dim dataKeyList As ArrayList = New ArrayList
                Dim grdParent As GridView = CType(Me.Control, GridView)
                If (grdParent.DataKeys.Count > 0) Then
                    For Each selectedIndex As Int32 In SelectedIndexes
                        Dim DataKey As Object = grdParent.DataKeys(selectedIndex).ToString
                        dataKeyList.Add(DataKey)
                    Next
                End If
                Return CType(dataKeyList.ToArray(GetType(System.Object)), Object())
            End Get
        End Property
    
        Public Overrides Sub InitializeCell(ByVal cell As DataControlFieldCell, ByVal cellType As DataControlCellType, ByVal rowState As DataControlRowState, ByVal rowIndex As Integer)
            If cell Is Nothing Then
                Throw New ArgumentNullException("cell", "cell is null.")
            End If
            MyBase.InitializeCell(cell, cellType, rowState, rowIndex)
            If (cellType = DataControlCellType.Header) OrElse (cellType = DataControlCellType.DataCell) Then
                Dim checkbox As CheckBox = New CheckBox
                If cellType = DataControlCellType.Header Then
                    checkbox.ID = "checkboxHead"
                Else
                    checkbox.ID = "checkboxCol"
                End If
                cell.Controls.Add(checkbox)
            End If
        End Sub
    
        Public Shared Sub RegisterClientCheckEvents(ByVal pg As Page, ByVal formID As String)
            If pg Is Nothing Then
                Throw New ArgumentNullException("pg", "pg is null.")
            End If
            If formID Is Nothing OrElse formID.Length = 0 Then
                Throw New ArgumentException("formID is null or empty.", "formID")
            End If
            Dim strCol As String = GetCheckColScript()
            Dim strHead As String = GetCheckHeadScript()
            If Not pg.ClientScript.IsClientScriptBlockRegistered("clientScriptCheckAll") Then
                pg.ClientScript.RegisterClientScriptBlock(pg.GetType, "clientScriptCheckAll", strHead.Replace("[frmID]", formID))
            End If
            If Not pg.ClientScript.IsClientScriptBlockRegistered("clientScriptCheckChanged") Then
                pg.ClientScript.RegisterClientScriptBlock(pg.GetType, "clientScriptCheckChanged", strCol.Replace("[frmID]", formID))
            End If
            RegisterAttributes(pg)
        End Sub
    
        Private Shared Sub RegisterAttributes(ByVal ctrl As Control)
            For Each wc As Control In ctrl.Controls
                If wc.HasControls Then
                    RegisterAttributes(wc)
                End If
                If TypeOf (wc) Is CheckBox Then
                    Dim chk As CheckBox = DirectCast(wc, CheckBox)
                    If Not chk Is Nothing AndAlso chk.ID = "checkboxCol" Then
                        chk.Attributes.Add("onclick", "CheckChanged()")
                    ElseIf Not chk Is Nothing AndAlso chk.ID = "checkboxHead" Then
                        chk.Attributes.Add("onclick", "CheckAll(this)")
                    End If
                End If
            Next
        End Sub
    
        Private Shared Function GetCheckColScript() As String
            Dim strScript As String
            strScript = " <script language=JavaScript>"
            strScript &= " function CheckAll( checkAllBox )"
            strScript &= " {"
            strScript &= " var frm = document.[frmID];"
            strScript &= "  var ChkState=checkAllBox.checked;"
            strScript &= "  for(i=0;i< frm.length;i++)"
            strScript &= "  {"
            strScript &= "         e=frm.elements[i];"
            strScript &= "        if(e.type=='checkbox' && e.name.indexOf('checkboxCol') != -1)"
            strScript &= "            e.checked= ChkState ;"
            strScript &= "  }"
            strScript &= " }"
            strScript &= "  </script>"
            Return strScript
        End Function
    
        Private Shared Function GetCheckHeadScript() As String
            Dim strScript As String
            strScript = "<script language=JavaScript>"
            strScript &= "function CheckChanged()"
            strScript &= "{"
            strScript &= "  var frm = document.[frmID];"
            strScript &= "  var boolAllChecked;"
            strScript &= "  boolAllChecked=true;"
            strScript &= "  for(i=0;i< frm.length;i++)"
            strScript &= "  {"
            strScript &= "    e=frm.elements[i];"
            strScript &= "  if ( e.type=='checkbox' && e.name.indexOf('checkboxCol') != -1 )"
            strScript &= "      if(e.checked== false)"
            strScript &= "      {"
            strScript &= "         boolAllChecked=false;"
            strScript &= "         break;"
            strScript &= "      }"
            strScript &= "  }"
            strScript &= "  for(i=0;i< frm.length;i++)"
            strScript &= "  {"
            strScript &= "    e=frm.elements[i];"
            strScript &= "    if ( e.type=='checkbox' && e.name.indexOf('checkboxHead') != -1 )"
            strScript &= "    {"
            strScript &= "      if( boolAllChecked==false)"
            strScript &= "         e.checked= false ;"
            strScript &= "      else"
            strScript &= "         e.checked= true;"
            strScript &= "      break;"
            strScript &= "    }"
            strScript &= "   }"
            strScript &= " }"
            strScript &= " </script>"
            Return strScript
        End Function
    End Class