ASP.Net State Question

I

Islamegy®

Hello,
In my web application (C#) i have 2 questions related to collection state:

1- I have a Datagrid Bounded to custom Collection.. Everytime i add new item
i can see this item in the Grid but after add new item i found only the last
one even Datagrid viewstate is Enabled..
I think the problem is in my Collection which have [Serializable]
attribute.. I tried to add my collection to cache and viewstate but the
problem is still there, How could i save my collections state??

2- When i add my collection to the cache and add new item to the collection
is this apply to the cache or i have to add it back again to the cache or
view state (Is it a refrence or new instance)?? and what about viewstate is
it work in the same way as cache and what is the diffrnce between cache &
session coz as i know use the memory??

thanx
 
B

Brock Allen

1- I have a Datagrid Bounded to custom Collection.. Everytime i add
new item
i can see this item in the Grid but after add new item i found only
the last
one even Datagrid viewstate is Enabled..
I think the problem is in my Collection which have [Serializable]
attribute.. I tried to add my collection to cache and viewstate but
the
problem is still there, How could i save my collections state??

I'm going to guess (since I'm having a hard time understanding the exact
problem) that you're databinding your DataGrid in Page_Load and then you're
adding a new item into your collection in a button click event. The Button
click event happens after Page_Load and that would explain why the grid doesn't
show the newly added item. In your button click event rebuild the datagrid
and it should have the newly added item.
2- When i add my collection to the cache and add new item to the
collection is this apply to the cache or i have to add it back again
to the cache or view state (Is it a refrence or new instance)?? and
what about viewstate is it work in the same way as cache and what is
the diffrnce between cache & session coz as i know use the memory??

You should not have to add data to ViewState manually on behalf if a control.
IOW, for a Label, once you set the Text property and its ViewStateEmnabled=true
it should maintain that data, not you. ViewState is so that a single page
and controls set with particular values maintain those values across postback.
Cache is a different thing. It's a server side property bag for frequently
used shared data (shared cross page and user, typically). They serve different
purposes.

-Brock
DevelopMentor
http://staff.develop.com/balle
 
J

john_teague

your collection will be an object type which is "reference by value".
For your case, if you change a reference to to your cached object, your
object will be changed too. But as Brock said, you have to rebind your
grid after the change regardless of how you are storing the data.
 

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

Top