為DataGrid新增CheckBox控制元件 (轉)

amyz發表於2007-08-15
為DataGrid新增CheckBox控制元件 (轉)[@more@]

為DataGrid新增CheckBox

playground.com/">

x" target=_blank>檢視例子

CheckBoxDataGrid.

.CheckBoxDataGrid&quot; %&gt; w3c//DTD HTML 4.0 Transitional//EN&quot;&gt; <title>為DataGrid新增CheckBox控制元件的例子</title> <meta name="GENERATOR" content="a href=" tag-126-1.html="">microsoft .NET 7.0&quot;&gt; <meta name="CODE_LANGUAGE" content="a href=" tag-50-1.html=""> 7.0&quot;&gt; <meta name="vs_defaultClientScript" content="a href=" tag-133-1.html="">script&quot;&gt; <meta name="vs_targetSchema" content=" href=" tag-340-1.html="">intellisense/&quot;&gt; <fo href="http://blog.itpub.net/10752019/viewspace-957929/tag-415-1.html">RM id=&quot;frmMain&quot; method=&quot;post&quot; runat=&quot;server&quot;&gt; edItemStyle Font-Size=&quot;9pt&quot; Font-Names=&quot;宋體&quot;&gt; <edititemstyle font-size="9pt" font-names="宋體" font-bold="True" forecolor="Red" backcolor="Info"> Public Class CheckBoxDataGrid Inherits System.Web.UI.Page Protected WithEvents cmdSelectAll As System.Web.UI.WebControls.Button Protected WithEvents dgMain As System.Web.UI.WebControls.DataGrid Protected WithEvents cmdFindSelected As System.Web.UI.WebControls.Button Dim oDataView As DataView Protected WithEvents Label1 As System.Web.UI.WebControls.Label Dim nnectionString As String = &quot;Provr=Microsoft.Jet.OLEDB.4.0;Data =&quot;_ + Server.MapPath(&quot;Test.mdb&quot;) #Region &quot; Web Form Designer Generated Code &quot; 'This call is required by the Web Form Designer. <system href="http://blog.itpub.net/10752019/viewspace-957929/tag-268-1.html">buggerStepThrough()&gt; Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System., ByVal e As System.EventArgs)_ Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles MyBase.Load 'Put user code to initialize the page here dgMain.Columns(0).HeaderText = &quot;選項&quot; dgMain.Columns(1).HeaderText = &quot;序號&quot; dgMain.Columns(2).HeaderText = &quot;標題&quot; cmdFindSelected.Text = &quot;檢視選中的專案&quot; RefreshGrid() If Not Page.IsPostBack Then cmdSelectAll.Text = &quot;全部選中&quot; dgMain.DataBind() End If End Sub #Region &quot;處理多選&quot; Private Sub cmdSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles cmdSelectAll.Click selectAll() End Sub Private Sub selectAll() Dim oDataGridItem As DataGridItem Dim chkExport As System.Web.UI.WebControls.CheckBox If cmdSelectAll.Text = &quot;全部選中&quot; Then For Each oDataGridItem In dgMain.Items chkExport = oDataGridItem.FindControl(&quot;chkExport&quot;) chkExport.Checked = True Next cmdSelectAll.Text = &quot;全部不選&quot; Else For Each oDataGridItem In dgMain.Items chkExport = oDataGridItem.FindControl(&quot;chkExport&quot;) chkExport.Checked = False Next cmdSelectAll.Text = &quot;全部選中&quot; End If End Sub #End Region #Region &quot;DataGrid&quot; Private Sub RefreshGrid() Dim oConnection As OleDbConnection Dim oCommand As OleDbDataAdapter Dim oDataSet As New DataSet() Try Dim s As String = &quot;Select top 5 * from TestTable order by id&quot; oConnection = New OleDbConnection(sConnectionString) oCommand = New OleDbDataAdapter(sSQL.ToString, oConnection) oCommand.Fill(oDataSet, &quot;TestTable&quot;) oDataView = New DataView(oDataSet.Tables(&quot;TestTable&quot;)) dgMain.DataSource = oDataView oConnection.Close() Catch ex As Exception '// Place Error Handling here End Try End Sub #End Region Private Sub cmdFindSelected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles cmdFindSelected.Click Dim oDataGridItem As DataGridItem Dim chkExport As System.Web.UI.WebControls.CheckBox Dim oExArgs As New System.Collections.ArrayList() Dim sID As String Label1.Text = &quot;&quot; For Each oDataGridItem In dgMain.Items chkExport = oDataGridItem.FindControl(&quot;chkExport&quot;) If chkExport.Checked Then Label1.Text = &quot;&quot; sID = CType(oDataGridItem.FindControl(&quot;lblColumn&quot;), Label).Text oExArgs.Add(sID) Dim i As Integer = 0 For i = 0 To oExArgs.Count - 1 Label1.Text += oExArgs(i) + &quot;,&quot; Next End If Next End Sub End Class </system></edititemstyle></fo>

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-957929/,如需轉載,請註明出處,否則將追究法律責任。

相關文章