ASP.NET 4.0 VB Grid视图在排序后未获取ID

时间:2014-02-11 13:40:13

标签: asp.net vb.net sorting gridview

当我对网格视图进行排序时,UniqueID列保持原样。我正在使用单独的页面进行编辑,当我将UniqueID传递给该页面以加载记录时,由于UniqueID是排序之前的位置,因此它得到了错误的记录。我在其他地方阅读有关排序和绑定的内容,但似乎这就是我正在做的事情。任何想法?

    Private Sub GridView2_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView2.Sorting

    Dim oldExpression As String = GridView2.SortExpression
    Dim newExpression As String = e.SortExpression

    If Session("SortExpression") <> e.SortExpression Then
        Session("SortExpression") = e.SortExpression
        ViewState("SortDirection") = Nothing
    End If

    If (ViewState("SortDirection")) Is Nothing Then
        If ViewState("SortDirection") = "ASC" Then
            ViewState("SortDirection") = "DESC"
        Else
            ViewState("SortDirection") = "ASC"
        End If
    Else
        If ViewState("SortDirection") = "DESC" Then
            ViewState("SortDirection") = "ASC"
        Else
            ViewState("SortDirection") = "DESC"
        End If
    End If

    '12/10/13 DBP 
    Dim SQLOrderByString As String = ""
    If e.SortExpression = "DateAwarded" Then
        SQLOrderByString = " Order by  CAST(CONVERT(VARCHAR,DateAwarded,101)  AS  smalldatetime) " & ViewState("SortDirection")
    Else
        SQLOrderByString = " Order by " & e.SortExpression & " " & ViewState("SortDirection")
    End If

    'note gblSQLAddNewString is created on page load so the column names are consistant
    Dim SQLWhereString As String = "Where Organization = '" & Session("Organization") & "' "

    Session("gblOrderByString") = SQLOrderByString
    Dim SQLCombinedString As String = gblSQLAddNewString & SQLWhereString & SQLOrderByString

    Me.GridView2.DataSource = PopulateGridwithSQL(SQLCombinedString)

    Me.GridView2.DataBind()

End Sub

''-----------------附加代码--------- 在单击要排序的列标题之前,会话(“EditSQLRecordID”)具有正确的顶行ID。单击网格上的编辑按钮后排序后,我可以在网格中看到正确的ID。但是当我暂停代码并检查Session(“EditSQLRecordID”)变量时,它是不正确的。因此我得错了记录。编辑正在另一页上进行。

Private Sub GridView2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView2.SelectedIndexChanged

    Try
        ' get the RowID of the grid this allows the choosing of a particular column value in the row chosen
        Dim RowID As Integer = GridView2.SelectedRow.RowIndex + 1

        ' Set session var to the SQL database record ID the last column (15) contains the SQL unique record ID
        '    this will be used to grab the record for editing
        Session("EditSQLRecordID") = GridView2.Rows(GridView2.SelectedIndex).Cells(17).Text
        Response.Redirect("EditGrid.aspx", False)

    Catch ex As Exception

        Dim ErrorTitle As String = "Error"
        Dim PageName = System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.Url.AbsolutePath)
        Dim Retval2 As String = ErrorTrap(PageName & " - " & System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message, ErrorTitle)

    End Try

End Sub
--------------------- Editing ----------------
   Private Sub GridView2_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView2.RowEditing
    Try
        ' get the RowID of the grid this allows the choosing of a particular column value in the row chosen
        Dim RowID As Integer = GridView2.SelectedRow.RowIndex + 1

        ' Set session var to the SQL database record ID the last column (15) contains the SQL unique record ID
        '    this will be used to grab the record for editing
        Session("EditSQLRecordID") = GridView2.Rows(GridView2.SelectedIndex).Cells(17).Text
        Response.Redirect("EditGrid.aspx", False)

    Catch ex As Exception
        Dim ErrorTitle As String = "Error"
        Dim PageName = System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.Url.AbsolutePath)
        Dim Retval2 As String = ErrorTrap(PageName & " - " & System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message, ErrorTitle)


    End Try

End Sub

-----------已添加--------------      Private Sub GridView2_PageIndexChanging(ByVal sender As Object,ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)处理GridView2.PageIndexChanging

    Me.GridView2.PageIndex = e.NewPageIndex

    ' 12/10/13 DBP 
    'note gblSQLAddNewString is created on page load so the column names are consistant
    Dim SQLWhereString As String = "Where Organization = '" & Session("Organization") & "' "

    ' 12-24-13 DBP added global sort since the Pages were out of order when sorting
    Dim SQLOrderByString As String = Session("gblOrderByString")
    '"Order by " & e.SortExpression & " " & ViewState("SortDirection")
    Dim SQLCombinedString As String = gblSQLAddNewString & SQLWhereString & SQLOrderByString

    Me.GridView2.DataSource = PopulateGridwithSQL(SQLCombinedString)

    Me.GridView2.DataBind()

End Sub

0 个答案:

没有答案