|
-
May 16th, 2008, 07:35 AM
#1
Thread Starter
PowerPoster
[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.
-
May 16th, 2008, 07:42 AM
#2
Thread Starter
PowerPoster
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.
-
May 16th, 2008, 07:53 AM
#3
Thread Starter
PowerPoster
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.
-
May 16th, 2008, 07:55 AM
#4
Frenzied Member
Re: [RESOLVED] My gridview is sorting successfully
stop talking to yourself
-
May 16th, 2008, 08:02 AM
#5
Thread Starter
PowerPoster
Re: [RESOLVED] My gridview is sorting successfully
*You* were listening!
-
May 16th, 2008, 08:21 AM
#6
Frenzied Member
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.
-
May 16th, 2008, 08:38 AM
#7
Thread Starter
PowerPoster
Re: [RESOLVED] My gridview is sorting successfully
 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.
-
May 16th, 2008, 08:42 AM
#8
Thread Starter
PowerPoster
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!
-
May 16th, 2008, 08:43 AM
#9
Frenzied Member
Re: [RESOLVED] My gridview is sorting successfully
lol yeah I think I just read the same link... wasn't disagreeing just looking for opinion.
-
May 16th, 2008, 02:43 PM
#10
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
-
May 19th, 2008, 08:42 AM
#11
Thread Starter
PowerPoster
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:
 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.
-
May 19th, 2008, 09:47 AM
#12
Thread Starter
PowerPoster
Re: [RESOLVED] My gridview is sorting successfully
 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...
-
May 19th, 2008, 10:49 AM
#13
Thread Starter
PowerPoster
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???
-
May 19th, 2008, 11:03 AM
#14
Thread Starter
PowerPoster
Re: [RESOLVED] My gridview is sorting successfully
 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!)
-
May 19th, 2008, 12:50 PM
#15
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?
-
May 19th, 2008, 12:51 PM
#16
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.
-
May 19th, 2008, 01:03 PM
#17
Thread Starter
PowerPoster
Re: [RESOLVED] My gridview is sorting successfully
 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 )
-
May 19th, 2008, 01:09 PM
#18
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|