Results 1 to 18 of 18

Thread: [RESOLVED] My gridview is sorting successfully

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Resolved [RESOLVED] My gridview is sorting successfully

    But... (you knew there had to be a "but" otherwise I wouldn't be posting!)

    I have a page called DocSelection that links to a page called DocList. These are currently in ASP and I am migrating them.

    DocList needs access to DocSelection's variables. This was easily done in ASP. I did some research, found out this is also available in .NET 2.0 (the version I'm using, phew!) via some calls to Me.PreviousPage.FindControl().

    So let's say there's a database key that's selected on DocSelection via dropdownlists and some checkboxes of what kind of documents the user wants displayed. The first time I populated my grid, I have all these varaibles because DocList was just called from DocSelection. Then I want to see my gridview sorted by Date in the other direction (is ascending, want descending). This causes a postback and I apparently can't use PreviousPage anymore to get the values of the variables I need to get my database data.

    My gridview is sorting successfully right now in development because I have the data that I am losing hardcoded. How can I retain this data between postbacks to the second page?

    This is the code that runs when I sort:
    Code:
        Public Property GridViewSortDirection() As SortDirection
    
            Get
                If ViewState("sortDirection") Is Nothing Then
                    ViewState("sortDirection") = SortDirection.Ascending
                End If
    
                Return DirectCast(ViewState("sortDirection"), SortDirection)
            End Get
    
            Set(ByVal value As SortDirection)
                ViewState("sortDirection") = value
            End Set
        End Property
    
        Protected Sub gvDocList_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvDocList.Sorting
            Dim sortExpression As String = e.SortExpression
    
            If GridViewSortDirection = SortDirection.Ascending Then
    
                GridViewSortDirection = SortDirection.Descending
    
                SortGridView(sortExpression, "DESC")
            Else
                GridViewSortDirection = SortDirection.Ascending
    
                SortGridView(sortExpression, "ASC")
            End If
    
        End Sub
    
        Private Sub SortGridView(ByVal sortExpression As String, ByVal direction As String)
    
            ' You can cache the DataTable for improving performance 
    
            ' Dim dt As DataTable = GetData().Tables(0)
            GetDataForGrid()
            Dim dt As DataTable = ds.Tables(0)
    
            Dim dv As New DataView(dt)
    
            dv.Sort = sortExpression + " " + direction
    
            gvDocList.DataSource = dv
    
            gvdoclist.DataBind()
    
        End Sub
    Oh, I just noticed...If I took this code's author's advice about caching the data, I guess I wouldn't have to call the database again so that would solve the problem where I lose my variables, but how is the dataset cached? How can the dataset be retained during the postback?

    Thanks.

  2. #2

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: My gridview is sorting successfully

    I think I need a ViewState.

    I'm sorry I even posted this. It wasn't until I posted it that I saw the comment in the sort code I borrowed that you might want to cache the data, so now I see it is normal to lose your data during a postback unless you explicitly save it.

    Thanks.

  3. #3

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: My gridview is sorting successfully

    Just so you have the solution if someone finds this thread in the future...

    Here is my new sort code:
    Code:
        Private Sub SortGridView(ByVal sortExpression As String, ByVal direction As String)
    
            Dim dt As DataTable = ds.Tables(0)
    
            Dim dv As New DataView(dt)
    
            dv.Sort = sortExpression + " " + direction
    
            gvDocList.DataSource = dv
    
            gvdoclist.DataBind()
    
        End Sub
    And the reason I can do that is because I did this as the last instruction when I call my stored procedure:

    ViewState("ds") = ds

    Code:
        Sub GetDataForGrid()
    
            Dim connString As String = _
                ConfigurationManager.ConnectionStrings("ConnectMe").ConnectionString
    
            Dim conn As New SqlConnection(connString)
    
            Dim command As New SqlCommand()
            command.CommandType = CommandType.StoredProcedure
            command.Connection = conn
    
            command.CommandText = "up_DocList"
    
            command.Parameters.Add(New SqlParameter("@cstmr_addr_ik", Data.SqlDbType.Int))
            ' command.Parameters("@cstmr_addr_ik").Value = CustomerAddrIk
            command.Parameters("@cstmr_addr_ik").Value = 2589
    
    
            command.Parameters.Add(New SqlParameter("@toplvl_ik_list", Data.SqlDbType.VarChar, 32))
            command.Parameters("@toplvl_ik_list").Value = "3,1"
    
            Dim adapter As New SqlDataAdapter(command)
            Try
                adapter.Fill(ds)
            Catch ex As Exception
                Response.Write("GetDataForGrid Exception " + ex.Message)
                conn.Close()
                Exit Sub
            Finally
                conn.Close()
            End Try
    
            ViewState("ds") = ds
    
        End Sub
    And in the Page_Load:
    Code:
            If Not IsPostBack Then
    
                ' DocList.aspx needs the data on DocSelection.aspx
                If (Not IsNothing(Me.PreviousPage)) Then
    
                    Dim ddAgents As DropDownList = CType(Me.PreviousPage.FindControl("ddAgents"), DropDownList)
                    If (Not IsNothing(ddAgents)) Then
                        AgentIk = CInt(ddAgents.SelectedItem.Value)
                    End If
    
                    Dim ddCustomers As DropDownList = CType(Me.PreviousPage.FindControl("ddCustomers"), DropDownList)
                    If (Not IsNothing(ddCustomers)) Then
                        CustomerIk = CInt(ddCustomers.SelectedItem.Value)
                    End If
    
                     Dim ckDocumentTypes As CheckBoxList = CType(Me.PreviousPage.FindControl("ckDocumentTypes"), CheckBoxList)
                    If (Not IsNothing(ckDocumentTypes)) Then
                        ' Iterate throught the checkboxlist and glean the selections.  If no checkboxes were checked 
                        '  sDocumentTypesSelected will remain an empty string and will not factor into our selection
                        '  criteria.
                        sDocumentTypesSelected = ""
                        Dim i As Integer
                        For i = 0 To ckDocumentTypes.Items.Count - 1
                            If ckDocumentTypes.Items(i).Selected = True Then
                                If sDocumentTypesSelected <> "" Then
                                    sDocumentTypesSelected += ","
                                End If
                                sDocumentTypesSelected += ckDocumentTypes.Items(i).Value
                            End If
                        Next
                    End If
    
                End If
    
                PopulateGrid()
            Else
                ds = CType(ViewState("ds"), DataSet)
            End If
    I left out the gory details, but PopulateGrid() calls GetDataForGrid().

    So there you have it.

  4. #4
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [RESOLVED] My gridview is sorting successfully

    stop talking to yourself

  5. #5

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: [RESOLVED] My gridview is sorting successfully

    *You* were listening!

  6. #6
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [RESOLVED] My gridview is sorting successfully

    Is there a reason that you would use viewstate over session? I think I remember being told not to a while ago, though I might be wrong.

  7. #7

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: [RESOLVED] My gridview is sorting successfully

    Quote Originally Posted by Besoup
    Is there a reason that you would use viewstate over session? I think I remember being told not to a while ago, though I might be wrong.
    Whatever link I happened to land on when I was googling said to use ViewState if your dataset is small and use Session if it's large...there were other reasons but now I can't find the link. I get 24 rows back to my grid so the size seemed to indicate use ViewState. If you disagree let me know. One never knows what one is picking up on Google, but I trust this forum.

  8. #8

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: [RESOLVED] My gridview is sorting successfully

    Found it. Here was the advice: i'd use viewstate if it's user specific and is only used in one place AND the dataset isn't enormous. i'd use session if it is user specific, large, and used in many places. i'd use the global cache is it's not user specific. And this the link if you want to read the whole thread: http://forums.asp.net/t/592006.aspx

    Comments?

    p.s. I'm not talking to myself anymore. Na na na na na!

  9. #9
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [RESOLVED] My gridview is sorting successfully

    lol yeah I think I just read the same link... wasn't disagreeing just looking for opinion.

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] My gridview is sorting successfully

    That thread is not telling you everything. Situations like these don't have a holy grail solution. It's all about how you design your application to work and using session isn't always the best way to deal with it.

    Viewstate information gets sent back to the page in the user's browser. So large amounts of data in it can slow the page load by making the page significantly heavier. On the other hand, session variables are stored on the server in-memory. If you store large amounts of data in session variables and you have a whole bunch of users who are going to be using that application, then there will be a lot of memory allocated just to store the session data. So it's still not a good idea to store a lot of information in session variables. This is where data caching comes in. Caching does sit in the server's memory but its usefulness lies in the fact that if server resources start going low, the cached objects can be deleted. Various parameters can be configured for caching.

    However, having said all that, you shouldn't need to be storing data like this if you have a source of data available to you that isn't processor intensive. Assuming the usage of SQL Server, the best idea is to pass enough information between the pages so that the page can query the database and get the relevant information out each time. This, assuming that it isn't a heavy query like a hundred table cross join

  11. #11

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: [RESOLVED] My gridview is sorting successfully

    I understand, but my whole issue was I was losing the criteria in order to call the database again. I didn't necessarily mind having to get the dataset again, I just didn't know how to preserve the parameters for subsequent calls to my stored procedure since they came from the previous page the first time.

    Then I saw if I just preserved my dataset, I wouldn't need the parameters.

    You say:
    Quote Originally Posted by mendhak
    the best idea is to pass enough information between the pages so that the page can query the database and get the relevant information out each time
    so I guess I wasn't sure what to do once it's the second page posting back to itself. Save them in session variables - is that what you are recommending?

    Thanks.

  12. #12

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: [RESOLVED] My gridview is sorting successfully

    Quote Originally Posted by mendhak
    there will be a lot of memory allocated just to store the session data.
    If the memory's available? And if it's not available, one will get an out of memory exception, like the one I just got???

    I think it's time to look into the session variables...

  13. #13

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: [RESOLVED] My gridview is sorting successfully

    Well I just spent an hour rewriting my code to not save the dataset in ViewState and I am *still* getting an out of memory exception. No doubt this rewrite was for the better, but what do I need to do to not use up all the memory???

  14. #14

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: [RESOLVED] My gridview is sorting successfully

    Quote Originally Posted by MMock
    what do I need to do to not use up all the memory???
    Remove or comment out this:
    Code:
            ' ViewState("ds") = ds
    Even though I wasn't using it, I had accidentally left it in there! (This is the first time I've ever quoted myself!)

  15. #15
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] My gridview is sorting successfully

    What did you finally implement then? Is it working the way you want it to?

    Did you end up passing the information between the pages using a session variable?

  16. #16
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] My gridview is sorting successfully

    so I guess I wasn't sure what to do once it's the second page posting back to itself. Save them in session variables - is that what you are recommending?
    Pass it to the second page however you want - session, querystring. Once it's on the page, save in viewstate so that it's available across subsequent viewstates. If it's in the querystring, just read the querystring variable each time if required.

    Again, this depends; yes, it can get confusing but it always depends on the way you're doing things but go for what works for you.

  17. #17

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,566

    Re: [RESOLVED] My gridview is sorting successfully

    Quote Originally Posted by mendhak
    What did you finally implement then? Is it working the way you want it to?

    Did you end up passing the information between the pages using a session variable?
    Oh sorry, I said I was going to use session variables. This is how:

    Code:
        ' This variable is global to the class.
        Dim CustomerIk As Integer
    
    
    ' This is in Page_Load
            If Not IsPostBack Then
                ' DocList was called from DocSelection.  Get the values the user selected in DocSelection's DDLs
    
                Dim ddCustomers As DropDownList = CType(Me.PreviousPage.FindControl("ddCustomers"), DropDownList)
    
                Session("ddCustomerSelected") = "0"
                If Not IsNothing(ddCustomers.SelectedItem) Then
                    If CInt(ddCustomers.SelectedIndex) > 0 Then
                        Session("ddCustomerSelected") = CInt(ddCustomers.SelectedItem.Value)
                    End If
                End If
    
            End If
    
            CustomerIk = CInt(Session("ddCustomerSelected").ToString)
    So I use the PreviousPage property when it's not a postback and the session variables when it is. Is that a good way? I'm happy with the way it's working, especially the not running out of memory part (I also had a logic error calling my stored proc and was getting 156,000 rows by mistake )

  18. #18
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] My gridview is sorting successfully

    Ah I see. A dropdownlist on a previous page. So there'd be two ways that I would use to approach this.

    First way is to get the dropdownlist event on the previous page to Response.Redirect("NewPage.aspx?selectedcustomer=" & the dropdownlist value). Then in the second page, just read Request.QueryString("selectedcustomer")

    The other way is what you've done, PreviousPage.FindControl, then put it into a ViewState variable, just keep reading the ViewState variable each time you need it.

    Before you go berzerk, I didn't say use a session variable! I was just giving general advice. You can do it the way you've done it right now, it's not going to give you problems. Right now I'm just telling you of various approaches to the same scenario.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width