ASP.Net 2.0 checkboxlist control losing all data after post back.

I have a page with an ASP.Net  2.0 checkboxlist control. On the initial page load (ispostback=false) I load the values into the control. After a post back all the list items are gone. I have view state enabled. Has anyone seen this behavior and/or know a work around?
Who is Participating?
Ok do it this way.

Session["ds"] = Ds //Ds = your firstly retrived value

When its posted back
retrive dataset from session.

DataSet ds = (DataSet)Session["ds"];

Thats it
Try taking out the "ispostback=false" section.

What should happen is you reload them in page_load, then viewstate will kick in and modify the values, then in your event it will have the new values.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

You have to clear them out from the ispostback block.
After post back it will remove all the values and clear your control.
reecypAuthor Commented:
Isn't there a way to avoid reloading the values and still keep the user's selection? If I'm understanding it correctly all the solutions above do so.
Another way is you can make an array of items and store them in session and selected value in one session variable.

Than load all these stuff from session on post back. But again it will be  same thing.
reecypAuthor Commented:
Does an ASP DropDownList control act differently from a CheckBoxList control? I ahve another page that uses the same coding concept and the values are not cleared on post back.


  If Not Page.IsPostBack Then
            Me.sqlLocations.SelectCommand = "SELECT * from table"
            Me.llocations.DataSource = dataSourceNameHere
            Me.llocations.DataTextField = "textfield"
            Me.llocations.DataValueField = "valuefield"
        End If
reecypAuthor Commented:
I think I Just discovered the issue...I am not binding to a datasource, but calling a routine that returns a dataset. I would assume that this is why the values are getting cleared. When using a datasource I am assuming that that the records are cached in viewstate therefore the records are retained.

Can anyone verify this?
This is the best read i've found about viewstate.   Scroll down to "3. Persisting cheap data"

Is there any reason you have to use viewstate?

The blog pretty much says, whats more efficent.  Serialize the data into viewsatate, send that to the client, the client sends it back for you to deserialize it, then load it back into the control to pull out a value, or pull back the data directly from your database.

And a quote form that link
"You might argue that making a database query on every request is MORE costly than persisting the data through ViewState. Modern database systems (say, SQL Server) have sophisticated caching mechanism and are extremely efficient if configured correctly. The state list needs to be repopulated on every request no matter what you're doing. All you've done is change it from being pushed and pulled down a slow, unreliable 56kbps internet connection that may have to travel for thousands of miles, to being pulled over at worse a 10 megabit LAN connection a couple hundred feet between your internet server and database server. AND if you really wanted to improve things, you could cache the results of the database query in the application."
Alpesh PatelCommented:
when a page been past back current value of each control stored in viewstate
after postback this values will assigned to perticular control...... this is the general page cycle.
now in your case after postback your server try to find out the Datalist in Checkboxlist... but at the time of postback there is no data in the list of checkbox, bcoz u didnt atteched Datasource to checkboxlist in postback.

so after postback evey time u need to bind datasource with the control to get all list of datas and viewstate values

so just remove if statement which check the postback condition.
make sure remove only if statement not all code inside the if statement

conclusion is that u need to bind Datasource to all the control on a page , which gives list of data , at every postback.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.