利用Session紀錄datagrid模板列中CheckBox的狀態 (轉)

worldblog發表於2007-08-17
利用Session紀錄datagrid模板列中CheckBox的狀態 (轉)[@more@]

 

在DataGrid 的模板列中加入CheckBox ,如果對DataGrid設定分頁,前一頁已經選中的CheckBox在回到原頁時,CheckBox的狀態會變為初始狀態。:namespace prefix = o ns = "urn:schemas--com::office" />

  如果想儲存checkbox的狀態,則可以用Session儲存,我是這樣實現的:

  1、首先,建立DataGrid.

 <:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 12px; POSITION: absolute; TOP: 88px" runat="server" AllowPaging="True" Width="416px" Height="248px">

   

   

   

   

   

     

     

     

     

     

   

   

 

  2、在Global.asax中,Session_Start事件中建立Session

Sub Session_Start(ByVal sender As , ByVal e As EventArgs)
   ' 在會話啟動時激發
   Dim checks As Boolean() = New Boolean(10000) {}
   Dim i As Integer
   For i = 0 To 10000
   checks(i) = False
   Next
  Session.Add("CheckBoxchecks", checks)
   End Sub

3.在DataGrid的PageIndd事件中:

 Private Sub DataGrid1_PageIndexChanged(ByVal As Object, ByVal e As System..UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged

  Dim count As Integer

  Dim cnn As Ole.OleDbConnection = New OleDb.OleDbConnection("Provr=Microsoft.Jet.OLEDB.4.0;Data Source=c:UserLog.mdb")

  Dim da As New OleDb.OleDbDataAdapter(" * from mm", cnn)

 

  da.Fill(dst)

 

  count = dst.Tables(0).Rows.Count  'DataGrid中的記錄總數;

  Dim check As Boolean()

  check = Me.Session("checkboxchecks")  'check()是記錄checkbox狀態的陣列,用session儲存。

  Dim j As Integer

  For j = 0 To DataGrid1.PageSize - 1

  Dim Che As CheckBox = DataGrid1.Items(j).Cells(0).FindControl("CheckBox1")

  If Not Che Is Nothing Then

  If Che.Checked = True Then

  check(DataGrid1.CurrentPageIndex * DataGrid1.PageSize + j) = True

  Else

  check(DataGrid1.CurrentPageIndex * DataGrid1.PageSize + j) = False

  End If

  End If

  Next

 DataGrid1.CurrentPageIndex = e.NewPageIndex

  Dim ds As New DataSet()

  da.Fill(ds, "a")

  DataGrid1.DataSource = ds

  DataGrid1.DataBind()

  Dim i As Integer

  For i = 0 To DataGrid1.PageSize - 1

  Dim Cx2 As CheckBox = DataGrid1.Items(i).Cells(0).FindControl("CheckBox1")

  If check(DataGrid1.CurrentPageIndex * DataGrid1.PageSize + i) = True Then

   Cx2.Checked = True

  Else

  Cx2.Checked = False

  End If

  Next

 

  End Sub

  這樣就可以利用Session實現checkbox翻頁後的狀態儲存問題.


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

相關文章