Use session to record the status of a checkbox in a DataGrid template column

Source: Internet
Author: User

A checkbox is added to the template column of the DataGrid, and if paging is set on the DataGrid, the checkbox's state changes to its original state when the checkbox on the previous page is returned to the primary page. <?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>

If you want to save the status of the checkbox, you can save it with the session, which I implemented:

1. First, set up the DataGrid.

<asp:datagrid id= "DataGrid1" style= "Z-INDEX:101; left:12px; Position:absolute; top:88px "runat=" Server "allowpaging=" True "width=" 416px "height=" 248px ">

<alternatingitemstyle backcolor= "#EEEEEE" ></AlternatingItemStyle>

<itemstyle font-size= "12px" font-names= "Song Body" ></ItemStyle>

<footerstyle horizontalalign= "Center" ></FooterStyle>

<Columns>

<asp:TemplateColumn>

<ItemTemplate>

<asp:checkbox id= "CheckBox1" runat= "Server" ></asp:checkbox>

</ItemTemplate>

</asp:TemplateColumn>

</Columns>

</asp:datagrid>

2. In the Global.asax file, the Session is established in the Session_Start event

Sub session_start (ByVal sender as Object, ByVal e as EventArgs)
' Fires when the session starts
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. In the PageIndexChanged event of the DataGrid:

Private Sub datagrid1_pageindexchanged (ByVal source as Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles datagrid1.pageindexchanged

Dim count as Integer

Dim CNN as Oledb.oledbconnection = New oledb.oledbconnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/ Userlog.mdb ")

Dim da as New oledb.oledbdataadapter ("select * from mm", CNN)

Da. Fill (DST)

Count = DST. Tables (0). Rows.Count ' Total number of records in the DataGrid;

Dim Check as Boolean ()

Check = Me.session ("checkboxchecks") ' Check () is an array of record checkbox states, saved with the 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 was 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

in this way, you can use the session to implement the State saving problem after the CheckBox page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.