none
gridview使用排序的問題 RRS feed

  • 問題

  • 各位大大好,
    我在gridview中放了checkbox,但是如果我按排序後,checkbox打勾的項目就不會繼續打勾,
    我要怎麼樣才能夠讓打勾的項目在排序後依然可以打勾呢?
    我的程式碼如下:
    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
            Dim i As Integer
            '暫存現在選取的
            Dim chk As CheckBox
            Dim temp As String
            temp = ""
            For i = 0 To GridView1.Rows.Count - 1
                If Me.GridView1.Rows(i).Cells(0).FindControl("Checkbox1") IsNot Nothing Then
                    chk = Me.GridView1.Rows(i).Cells(0).FindControl("CheckBox1")
                    If chk.Checked Then
                        temp += Trim(Me.GridView1.Rows(i).Cells(2).Text) & ","
                    End If
                End If
            Next
            temp = temp.TrimEnd(",")
            Dim temp_ary() As String
            temp_ary = temp.Split(",")

            SqlDataSource4.SelectCommand = "SELECT * FROM itembank"
            GridView1.DataBind()

            Dim chk2 As CheckBox
            For i = 0 To temp_ary.Count - 1
                If temp_ary(i) <> "" Then
                    For j = 0 To GridView1.Rows.Count - 1
                        If GridView1.Rows(j).Cells(2).Text = temp_ary(i) Then
                            chk2 = Me.GridView1.Rows(j).Cells(0).FindControl("CheckBox1")
                            chk2.Checked = True
                        End If
                    Next
                End If
            Next
     End Sub
    謝謝各位大大的協助~

    2010年2月5日 下午 12:34

解答

  • Dim rememberState As Boolean = False
    Dim temp_ary() As String
    
    Protected Sub GridView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PreRender
    	If Not rememberState Then Exit Sub
    
    	Dim chk2 As CheckBox
    	For i = 0 To temp_ary.Length - 1
    		If temp_ary(i) <> "" Then
    			For j = 0 To GridView1.Rows.Count - 1
    				If GridView1.Rows(j).Cells(2).Text = temp_ary(i) Then
    					chk2 = Me.GridView1.Rows(j).Cells(0).FindControl("CheckBox1")
    					chk2.Checked = True
    				End If
    			Next
    		End If
    	Next
    End Sub
    
    
    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
    	rememberState = True
    	Dim i As Integer
    	'暫存現在選取的
    	Dim chk As CheckBox
    	Dim temp As String
    	temp = ""
    	For i = 0 To GridView1.Rows.Count - 1
    		If Me.GridView1.Rows(i).Cells(0).FindControl("Checkbox1") IsNot Nothing Then
    			chk = Me.GridView1.Rows(i).Cells(0).FindControl("CheckBox1")
    			If chk.Checked Then
    				temp += Trim(Me.GridView1.Rows(i).Cells(2).Text) & ","
    			End If
    		End If
    	Next
    	temp = temp.TrimEnd(",")
    
    	temp_ary = temp.Split(",")
    
    	SqlDataSource4.SelectCommand = "SELECT [ArticleID], [ArticleID], [AddedBy], [CategoryID], [Title], [Path] FROM TheBeerHouse.[Articles]"
    	GridView1.DataBind()
    End Sub
    • 已標示為解答 skywalk01 2010年2月6日 上午 05:10
    2010年2月5日 下午 01:38

所有回覆

  • Dim rememberState As Boolean = False
    Dim temp_ary() As String
    
    Protected Sub GridView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PreRender
    	If Not rememberState Then Exit Sub
    
    	Dim chk2 As CheckBox
    	For i = 0 To temp_ary.Length - 1
    		If temp_ary(i) <> "" Then
    			For j = 0 To GridView1.Rows.Count - 1
    				If GridView1.Rows(j).Cells(2).Text = temp_ary(i) Then
    					chk2 = Me.GridView1.Rows(j).Cells(0).FindControl("CheckBox1")
    					chk2.Checked = True
    				End If
    			Next
    		End If
    	Next
    End Sub
    
    
    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
    	rememberState = True
    	Dim i As Integer
    	'暫存現在選取的
    	Dim chk As CheckBox
    	Dim temp As String
    	temp = ""
    	For i = 0 To GridView1.Rows.Count - 1
    		If Me.GridView1.Rows(i).Cells(0).FindControl("Checkbox1") IsNot Nothing Then
    			chk = Me.GridView1.Rows(i).Cells(0).FindControl("CheckBox1")
    			If chk.Checked Then
    				temp += Trim(Me.GridView1.Rows(i).Cells(2).Text) & ","
    			End If
    		End If
    	Next
    	temp = temp.TrimEnd(",")
    
    	temp_ary = temp.Split(",")
    
    	SqlDataSource4.SelectCommand = "SELECT [ArticleID], [ArticleID], [AddedBy], [CategoryID], [Title], [Path] FROM TheBeerHouse.[Articles]"
    	GridView1.DataBind()
    End Sub
    • 已標示為解答 skywalk01 2010年2月6日 上午 05:10
    2010年2月5日 下午 01:38
  • 可以了~~
    真是太感謝阿尼大大了~~
    感謝~!
    2010年2月6日 上午 05:11