Link to home
Start Free TrialLog in
Avatar of Robb Hill
Robb HillFlag for United States of America

asked on

Gridview with Paging and Checkboxes

I have made a web user control which is a asp.net 2.0 gridview.  Within the control I have created the ability for paging and checkboxes.  I have writen code behind to remember the values that are checked so that as you use paging the values are not forgotten at postback.  What is strange when debugging my code is everything seems to be working...and then at the last second the checkbox dissapears....I cannot seem to catch any event that is triggering this.   I will make two further posts...one will be my code behind and the other with be the source.   Please help
Avatar of Robb Hill
Robb Hill
Flag of United States of America image

ASKER

<asp:GridView ID="grdSearchResults" runat="server"  DataSourceID="dsSearchResults" AutoGenerateColumns="False"
 AllowPaging="True"  PageSize="5" Width="324px"  DataKeyNames="TECHID"
 OnPageIndexChanging="grdSearchResults_PageIndexChanging">
    <Columns>
        <asp:TemplateField HeaderText="Select">
              <ItemTemplate>
                   <asp:CheckBox ID="CheckBox1"  runat="server" />
              </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="TECHID" HeaderText="Tech ID"/>
        <asp:BoundField DataField="NAME" HeaderText="Name"  />
        <asp:BoundField DataField="Address1" HeaderText="Office Address" />
        <asp:BoundField DataField="PHONE1" HeaderText="Primary Phone" />
        <asp:BoundField DataField="TECHSTAT" HeaderText="Status"  />          
    </Columns>
    <SelectedRowStyle BackColor="#FFFFC0" />
    <AlternatingRowStyle BackColor="#E0E0E0" />
</asp:GridView>
<asp:SqlDataSource ID="dsSearchResults" runat="server"
ConnectionString="<%$ ConnectionStrings:MidwareConnectionString %>"
 SelectCommand="select tc.TECHID,tc.[NAME],tc.Address1, tc.PHONE1,tc.TECHSTAT from
                     (select t.TECHID,t.[NAME],t.Address1, t.PHONE1,t.TECHSTAT,c.CALLNBR,c.SRVTYPE,c.State from cms..svc00100 t
                             left join cms..svc00200 c
                                on t.TECHID = c.TECHID)tc
                left join
                      (select pc.SvcCallNumber from midware..ProjectHeader ph
                             left join midware..ProjectSiteList ps
                               on ps.SiteList2Project = ph.objectid
                             left join midware..ProjectSvcCallList pc
                               on pc.SvcCall2ProjectSite = ps.objectid)p
                on p.SvcCallNumber = tc.CALLNBR">
   
</asp:SqlDataSource>
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
Imports System.Collections

Partial Class Members_TechSearchResults
    Inherits System.Web.UI.UserControl

    Dim strSelect As String = "select tc.TECHID,tc.[NAME],tc.Address1, tc.PHONE1,tc.TECHSTAT " _
                               & " from " _
                                    & " (select t.TECHID,t.[NAME],t.Address1, t.PHONE1,t.TECHSTAT,c.CALLNBR,c.SRVTYPE,c.State from cms..svc00100 t " _
                                    & "     left join cms..svc00200 c " _
                                    & "           on t.TECHID = c.TECHID)tc " _
                               & " left join " _
                                    & "  (select pc.SvcCallNumber from midware..ProjectHeader ph " _
                                    & "     left join midware..ProjectSiteList ps " _
                                    & "               on ps.SiteList2Project = ph.objectid " _
                                    & "     left join midware..ProjectSvcCallList pc " _
                                    & "             on pc.SvcCall2ProjectSite = ps.objectid)p " _
                               & " on p.SvcCallNumber = tc.CALLNBR"


    Private Const CHECKED_ITEMS As String = "CheckedItems"


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not Page.IsPostBack Then
            BindData()
        End If
    End Sub

    Private Sub BindData()
        dsSearchResults.SelectCommand = strSelect
        grdSearchResults.DataBind()
    End Sub


    Protected Sub grdSearchResults_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        ' Put old values in a session
        RememberOldValues()

        grdSearchResults.PageIndex = e.NewPageIndex
        BindData()

        RePopulateValues()
    End Sub

    Private Sub RePopulateValues()
        Dim categoryIDList As ArrayList = DirectCast(Session(CHECKED_ITEMS), ArrayList)

        If categoryIDList IsNot Nothing AndAlso categoryIDList.Count > 0 Then
            For Each row As GridViewRow In grdSearchResults.Rows
                Dim rowValue As String = grdSearchResults.DataKeys(row.RowIndex).Value

                If categoryIDList.Contains(rowValue) Then
                    Dim myCheckBox As CheckBox = DirectCast(row.FindControl("CheckBox1"), CheckBox)
                    myCheckBox.Checked = True
                End If
            Next
        End If

    End Sub

    Private Sub RememberOldValues()
        Dim categoryIDList As New ArrayList()
        Dim RowValue As String = ""
        For Each row As GridViewRow In grdSearchResults.Rows
            RowValue = grdSearchResults.DataKeys(row.RowIndex).Value

            Dim result As Boolean = DirectCast(row.FindControl("CheckBox1"), CheckBox).Checked

            ' Check in the Session
            If Session(CHECKED_ITEMS) IsNot Nothing Then
                categoryIDList = DirectCast(Session(CHECKED_ITEMS), ArrayList)
            End If

            If result Then
                If Not categoryIDList.Contains(RowValue) Then
                    categoryIDList.Add(RowValue)
                End If
            Else
                categoryIDList.Remove(RowValue)

            End If
        Next

        If categoryIDList IsNot Nothing AndAlso categoryIDList.Count > 0 Then
            Session(CHECKED_ITEMS) = categoryIDList
        End If
    End Sub
End Class

SOLUTION
Avatar of SystemExpert
SystemExpert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thats not working..same behavior...

Its strange...when you run the code...the very last thing I can see if the code actually tell the checkboxes to recheck based on whats stored in session...
then there is a flicker...and the checkboxes dissapear...its almost like there is a postback..but im catching it or something..I wonder could using this as a user control be the problem..i really have no idea...but this is crazy
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is a data grid solution....

I am using visual studio 2005 and attempting a solution using the asp.net 2.0 GRIDVIEW...

Any other ideas?
could the fact that this is a user control on a page within a master page be any factor in the extra flicker at the end that omits the check box
Well I havent had much feedback on this lately and moved on to something else...
Now I am back at square one with this issue:

Basically I am trying to store the values in session...what was checked...in an arralylist..
so when a user clicks from page to page what was checked is still there.

is there a better way to do this...and if not
how do u get this to work...I cant seem to get it work even using codeproject ect examples.
neglected question??
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial