Geeks With Blogs

Shelfari: Book reviews on your book blog

.net alternatives by Michel Grootjans

This is the point where I will be loosing major geek credits, but please bear with me.

My expectation here is that I set a list of objects as the DataSource of some data control on the first GET operation. When a POST operation occurs, I don't want to overwrite the user's modifictaion on my dataControl, so I expect it to load from the ViewState.

protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
        dataControl.DataSource = GetCustomers();
    Response.Write("dataControl.DataSource = " + dataControl.DataSource);

On PostBack, the control renders with the same data, but when I test the dataControl.DataSource, it is null.

First GET operation 

On postback 


Somewhere here, my reasoning is flawed. Where did I go wrong?

What do I need to do to capture the user's clicks on the checkboxes (other than enabling them ;-)?

Posted on Friday, December 19, 2008 9:02 AM | Back to top

Comments on this post: State in

# re: State in
Requesting Gravatar...
The solution to your problem: don't use but some kind of mvc framework. No more problems with viewstates and lifecycles :-)

And combine the mvc framework with the power of jquery :p

Left by Nathan on Dec 19, 2008 10:30 AM

# re: State in
Requesting Gravatar...
You're doing nothing wrong, really forgets what the datasource was. We just have to live with that. A solution I frequently use is storing your datasource manually in the viewstate, so you can still retrieve and manipulate it after postback.

Something like > ViewState["mydataSource"] = customerList;
I find it useful to use the datacontrol itself as the key > ViewState[dataControl.ClientID] = customerList.

After postback you can retrieve it with > List customerList = (List)ViewState[dataControl.ClientID]

You can then do fun stuff like comparing the original data with the changes the user made on your dataControl.
Left by Bjorn Verryt on Dec 19, 2008 3:14 PM

# re: State in
Requesting Gravatar...
Bjorn's suggestion will work, but there are some things to consider:

Viewstate can get big really easily, and while you *can* store stuff in there, remember that its going to get pushed to the client and back to the server so you'll have extra overhead when loading your page.

Another concern with putting the datasource into the viewstate is that you could be exposing sensitive data to the user. Although the viewstate is hashed its documented that its not a super-secure hash and can be decrypted with a bit of effort. With objects it may not be as big a deal, but if you were to use a sql data source which contains sql text for a select, delete, or update command, someone could get a glimpse of what the underlying table structure looks like.

If you absolutely need to compare the values from the originally databound controls, I'd suggest storing the objects in session instead of viewstate: it stays on the server so there's no extra markup overhead and its secure.

Left by D'Arcy from Winnipeg on Dec 21, 2008 5:58 AM

# re: State in
Requesting Gravatar...
Similar to D'Arcy's comments...
I think of it in this ASP.NET anything that is assigned is retained for only the lifespan of the page. The lifespan of the page is really only the execution of the page's life cycle. Once the page is loaded, the game is over. You may see the results of the life cycle, but that doesn't mean values for variables are retained unless you A) reassign them, or B) manually save the values in session, viewstate, cache, etc.

Also keep in mind regarding storing objects in session...this means you'll have to either wait for the user to timeout or call session.abandon() to clear these objects from your server's memory [assuming you're using InProc sessions]. That could be a problem, especially if you're storing other stuff in session for the user and don't want to clear that out. Sometimes I use cache with a sliding expiration (e.g., one minute) to get around this. That way I get my memory back a minute after the last time the object was accessed.

Sorry for the longish post, HTH :)
Left by SanjayU on Dec 21, 2008 11:00 PM

# re: State in
Requesting Gravatar...
@Nathan: That's exactly what I'm going to do -

I really can't be bothered by the viewstate anymore.
Left by Michel on Dec 22, 2008 2:12 PM

# re: State in
Requesting Gravatar...
take a look at mvc, microsoft has done a good job an that framework.
Left by Nathan on Jan 05, 2009 1:44 PM

Your comment:
 (will show your gravatar)

Copyright © Michel Grootjans | Powered by: