DataView, DataGrid and IsPostBack

E

enceladus311

I'm trying to find a way to keep from having to fill a DataView after
every PostBack to a page. Basically, the design is that I have a
DataView that I fill, which I then set as the DataSource to a DataGrid
on my page. This works well, however, like I said, I would like to
keep from having to fill the DataView on each PostBack. So, naturally,
what I did was checked whether or not the request was a PostBack by
checking the IsPostBack property. Sounds great, but it broke the
sorting and paging (probably other things) on my DataGrid. For
example, when I click on a column to sort by, the DataGrid disappears.

I did some quick debugging and added a watch and realized that my
problem comes from the fact that my DataView does not retain its value
between posts (at the beginning of each page load (between post backs)
the DataView contains an unknown (uninitialized) value).

<code>
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
if (Cache["ListAllViewableAuthors"] == null)
Cache["ListAllViewableAuthors"] =
DataLayer.Authors.ListAllViewableAuthors();

this.AuthorsDataView = new DataView((DataTable)
Cache["ListAllViewableAuthors"]);
this.AuthorsDataGrid.DataSource = this.AuthorsDataView;
this.AuthorsDataGrid.DataBind();
}
}
</code>

As you can see, I have seperated the database access layer and created
a static method to fill my DataView. But anyway, what is the proper
way to implement such a design so that I don't have to fill the
DataView on each post back?

Thank you in advance,
 
R

Remy

Looking at you code, it looks like you already do cache the
ListAllViewableAuthors. Since you do that, it's not much of an overhead
to just assign it everytime the page loads.
This is something I've seen done on other pages too, either cache it or
some people tend to stick it into the session or even the viewstate.
But if you do that you really need to know what you are doing. If the
list gets big neither viewstate or session is the place to go.

Cheers

Remy
 
S

S. Justin Gengo

There are only two things you could do to solve this:

1) Save to and retrieve the dataview from a session variable, but do you
really want to save the DataView in memory between postbacks? If you have a
lot of users then this solution could use up a lot of memory.

2) Store in and retrieve the dataview from the client side viewstate. But if
the dataview is large this could put a large delay between postbacks into
the application.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
F

Fao, Sean

Remy said:
Looking at you code, it looks like you already do cache the
ListAllViewableAuthors. Since you do that, it's not much of an overhead
to just assign it everytime the page loads.
This is something I've seen done on other pages too, either cache it or
some people tend to stick it into the session or even the viewstate.
But if you do that you really need to know what you are doing. If the
list gets big neither viewstate or session is the place to go.

That's good to know. I wasn't really too sure about the overhead
involved with caching. I'm glad to hear that it's little because this
DataTable could actually grow quite large.

Thank you for your feedback,
 
F

Fao, Sean

S. Justin Gengo said:
There are only two things you could do to solve this:

1) Save to and retrieve the dataview from a session variable, but do you
really want to save the DataView in memory between postbacks? If you have a
lot of users then this solution could use up a lot of memory.

2) Store in and retrieve the dataview from the client side viewstate. But if
the dataview is large this could put a large delay between postbacks into
the application.

The DataView could potentially grow quite large. I think caching is my
best answer. I was kind of hoping that I was missing something that
should have been obvious.

Thank you for your help,
 
R

Remy

My point was that you already cache it, but you do not assign it to the
ListView everytime. If it grows too big, then maybe caching is not a
good option anymore and the only way left is to get it everytime from
the db.
This really depends on your requirements. If speed is the most
important thing, then cache it and get an appropriate box. If you have
to balance it, then I would suggest you only cache lists up to a
certain size and get the big ones directly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top