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?
reecypAsked:
Who is Participating?
 
chinu1310Commented:
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
0
 
UnexplainedWaysCommented:
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.
0
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.

 
chinu1310Commented:
You have to clear them out from the ispostback block.
After post back it will remove all the values and clear your control.
0
 
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.
0
 
chinu1310Commented:
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.
0
 
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.

Example:

  If Not Page.IsPostBack Then
            Me.sqlLocations.SelectCommand = "SELECT * from table"
            'Me.sqlLocations.DataBind()
            Me.llocations.DataSource = dataSourceNameHere
            Me.llocations.DataTextField = "textfield"
            Me.llocations.DataValueField = "valuefield"
            Me.llocations.DataBind()
         
        End If
0
 
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?
0
 
UnexplainedWaysCommented:
This is the best read i've found about viewstate.   Scroll down to "3. Persisting cheap data"
http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx

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."
0
 
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.

thanx
0
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.