views:

15

answers:

1

I have a dynamic ASP.NET form which I recreate evertime I postback. My values persist no problem.

I am however having a challenge maintaining attributes on Postback. For example, I have user defined code which may enable or disable a field when someone is first presented with the form. If a user posts the form I need an easy way to make sure the field stays enabled or disabled.

Does this make sense? Is there an easy way to do this?

A: 

ViewState is the preferred method of persisting information between postbacks that doesn't need to live beyond the scope of a single page. You can store information pretty easily there.

An easy way to accomplish this is to use a property in the control, or the page that abstracts the use of ViewState from you.

protected Boolean IsFieldVisible
{
    get{ return (Boolean)ViewState["SomeUniqueKey"] ?? false; }
    set{ ViewState["SomeUniqueKey"] = value; }
}

This will maintain the value between postbacks.

Josh