DataView, DataGrid and IsPostBack

Discussion in 'ASP .Net' started by enceladus311@yahoo.com, Jan 6, 2006.

  1. Guest

    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,

    --
    Sean
    , Jan 6, 2006
    #1
    1. Advertising

  2. Remy Guest

    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
    Remy, Jan 6, 2006
    #2
    1. Advertising

  3. 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
    <> wrote in message
    news:...
    > 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,
    >
    > --
    > Sean
    >
    S. Justin Gengo, Jan 6, 2006
    #3
  4. Fao, Sean Guest

    Remy wrote:
    > 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,

    --
    Sean
    Fao, Sean, Jan 10, 2006
    #4
  5. Fao, Sean Guest

    S. Justin Gengo wrote:
    > 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,

    --
    Sean
    Fao, Sean, Jan 10, 2006
    #5
  6. Remy Guest

    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.
    Remy, Jan 10, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    4,746
    Patrick.O.Ige
    Nov 5, 2005
  2. Joey
    Replies:
    2
    Views:
    3,913
  3. TB
    Replies:
    0
    Views:
    484
  4. Replies:
    0
    Views:
    567
  5. CodeMonkey
    Replies:
    1
    Views:
    715
    joyal jhaveri
    Feb 4, 2011
Loading...

Share This Page