PDA

Click to See Complete Forum and Search --> : repeater, viewstate, and events - major problems!


pbabcock
Aug 17th, 2005, 11:21 AM
Hi all..

I have a repeater that is bound to an xml string containing shopping cart contents (results from a SQL query.) Inside the repeater, the various cart items are displayed along with delete button for each. All of this is shown using the standard repeater itemtemplates, etc. The problem I am having is with responding to the events when someone clicks on the "delete" button.

My situation, as I see it:
- I need to rebind the shopping cart repeater on every page because the shopping cart contents often change and I need it to reflect this. The page is full of "add to cart" buttons, and when one is clicked the shopping cart contents are changed.
- I can not limit my shopping cart repeater to rebind when IsPostBack=False, because the add to cart buttons cause a post back that could change the cart contents, meaning that I need to rebind even when the page IS a post back.
- Since I need to rebind all the time, I have disabled viewstate for the repeater.
- Because I have disabled viewstate, I can not react to any events for the repeater. Therefore I never receive any events when the delete button inside the repeater, is clicked, and I can not find another way to know when this is happening.

How do I get around this?? Am I doing this wrong?

ANY advice is appreciated!

dj4uk
Aug 18th, 2005, 08:52 AM
Right first thing separate all the code used to bind the repeater into a separate method so it can be called easily without the need to use more than one line of code - best practice as the same code will be used in lots of places.

Next only databind it initially when it is not a postback.

Finally to avoid the problems you are on about with respect to the contents changing regularly just call the data bind method you created every time anything is changed i.e. in the event for the add to basket button click add the data bind function as the last command to be executed.

Hope that all makes sense - it basically means the data bind function is called only when it needs to be rather than every time there is a postback. Note: rebinding every postback might actually be interferring with the events for the repeater.

Finally I think you might have misunderstood ViewState - as long as the repeater doesn't contain a massive amount of data I can see no reason to turn it off. I'm only aware of people turning off ViewState for A) security reasons and B) when there are a hell of a lot of controls on the page or very large amounts of data in controls (as the hidden ViewState in the HTML gets very large which slows down page request times due to physical page size).

Post back if you have any problems or further questions.

DJ

pbabcock
Aug 22nd, 2005, 01:16 PM
Thanks for your response. This makes sense.
I will try out your suggestions.