?
Solved

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

Posted on 2007-08-02
10
Medium Priority
?
4,515 Views
Last Modified: 2008-01-09
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?
0
Comment
Question by:reecyp
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 12

Expert Comment

by:UnexplainedWays
ID: 19616622
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
 
LVL 14

Expert Comment

by:chinu1310
ID: 19616815
You have to clear them out from the ispostback block.
After post back it will remove all the values and clear your control.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:reecyp
ID: 19623916
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
 
LVL 14

Expert Comment

by:chinu1310
ID: 19625286
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
 

Author Comment

by:reecyp
ID: 19625420
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
 

Author Comment

by:reecyp
ID: 19625530
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
 
LVL 14

Accepted Solution

by:
chinu1310 earned 750 total points
ID: 19627072
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
 
LVL 12

Assisted Solution

by:UnexplainedWays
UnexplainedWays earned 750 total points
ID: 19629718
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
 
LVL 10

Expert Comment

by:Alpesh Patel
ID: 20097182
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question