Robb Hill
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
ASKER
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.Add ress1, tc.PHONE1,tc.TECHSTAT " _
& " from " _
& " (select t.TECHID,t.[NAME],t.Addres s1, t.PHONE1,t.TECHSTAT,c.CALL NBR,c.SRVT YPE,c.Stat e 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..ProjectSvcCallLis t 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.SelectComm and = strSelect
grdSearchResults.DataBind( )
End Sub
Protected Sub grdSearchResults_PageIndex Changing(B yVal 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.RowInd ex).Value
If categoryIDList.Contains(ro wValue) Then
Dim myCheckBox As CheckBox = DirectCast(row.FindControl ("CheckBox 1"), 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.RowInd ex).Value
Dim result As Boolean = DirectCast(row.FindControl ("CheckBox 1"), 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(Ro wValue) Then
categoryIDList.Add(RowValu e)
End If
Else
categoryIDList.Remove(RowV alue)
End If
Next
If categoryIDList IsNot Nothing AndAlso categoryIDList.Count > 0 Then
Session(CHECKED_ITEMS) = categoryIDList
End If
End Sub
End Class
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.
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.Add
& " from " _
& " (select t.TECHID,t.[NAME],t.Addres
& " 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..ProjectSvcCallLis
& " 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.SelectComm
grdSearchResults.DataBind(
End Sub
Protected Sub grdSearchResults_PageIndex
' Put old values in a session
RememberOldValues()
grdSearchResults.PageIndex
BindData()
RePopulateValues()
End Sub
Private Sub RePopulateValues()
Dim categoryIDList As ArrayList = DirectCast(Session(CHECKED
If categoryIDList IsNot Nothing AndAlso categoryIDList.Count > 0 Then
For Each row As GridViewRow In grdSearchResults.Rows
Dim rowValue As String = grdSearchResults.DataKeys(
If categoryIDList.Contains(ro
Dim myCheckBox As CheckBox = DirectCast(row.FindControl
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(
Dim result As Boolean = DirectCast(row.FindControl
' Check in the Session
If Session(CHECKED_ITEMS) IsNot Nothing Then
categoryIDList = DirectCast(Session(CHECKED
End If
If result Then
If Not categoryIDList.Contains(Ro
categoryIDList.Add(RowValu
End If
Else
categoryIDList.Remove(RowV
End If
Next
If categoryIDList IsNot Nothing AndAlso categoryIDList.Count > 0 Then
Session(CHECKED_ITEMS) = categoryIDList
End If
End Sub
End Class
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
I am using visual studio 2005 and attempting a solution using the asp.net 2.0 GRIDVIEW...
Any other ideas?
ASKER
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
ASKER
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.
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.
ASKER
neglected question??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
AllowPaging="True" PageSize="5" Width="324px" DataKeyNames="TECHID"
OnPageIndexChanging="grdSe
<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:MidwareC
SelectCommand="select tc.TECHID,tc.[NAME],tc.Add
(select t.TECHID,t.[NAME],t.Addres
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..ProjectSvcCallLis
on pc.SvcCall2ProjectSite = ps.objectid)p
on p.SvcCallNumber = tc.CALLNBR">
</asp:SqlDataSource>