Page refresh and bind not working
I've got a .aspx and in it an .ascx. In the .ascx I have a server control.
The .aspx has a list of items in a cart The .ascx has a list of some cross-sell items (fed from a custom server control) that the customer can add to the cart if desired
both the list of items in cart and list of cross sell items are driven by a repeater and bound on page load. So the .aspx calls a method that rebinds the cart items on page load. And the .ascx calls a method in its page load that rebinds the cross sell items in that custom control (.cs) that is in my .ascx.
The problem I have is, when the user clicks a "add to cart" button in the repeater inside my custom control, the page refreshes and what should happen is the cart items in the .aspx AND the list of cross sells should refrsh showing that the cross sell item was moved to the cart. But eve开发者_C百科n though when I debug and I see the list being rebound with the corret # of items after the move, the page still shows the old state. I have to refresh the page manually again to get it to work.
I guess I need to check for Page.IsPostback? but even if I don't check that..at the least both lists should be refreshing regardless cause I have it in my page load. So even if it's a postback, and I'm not checking for that the lists should show the new state because I'm not even checking for postback anyway. So checking for postaback I don't think is the issue here cause i want the lists to rebind and re-update on any page load....initial or if it's a postback...it doesn't matter. Reload every time. But it doesn't seem to be doing this even though I clearly see the revbind of the lists having the right count (one less on the cross sell and one additional to the cart as it was added after the user clicked the button).
So lets recap again. Here's the sequence of events:
- User is on their cart page
- User sees a list of cart items that they have added to their cart already (this list is bound in the Cart.aspx and rebound on every page load)
- User sees a list of possible cross sell items they can add to their cart somewhere on the page. This list is a custom control found in my .ascx and that .ascx is obviously in my .aspx. The custom control is just a repeater that lists out the cross sell items
- User clicks one of the "Add to cart" buttons on one of the cross sells
- Page refreshes. In the page load of the .aspx AND the .ascx, I am going out to the DB and rebinding both those lists based on the new state...that is, the new lists after the item was added to the cart which means that now the cart items should have one more added to the list and it does...I clearly see that the new list has one more.
- The page comes back after the refresh but I don't see the lists reflecting the new state...I don't see the new item in the cart items list and the added cross sell removed from the cross sell list even though again both lists when I debug are showing correct set of records reflecting the new state
so I'm lost here as to why this is not showing me the updated results.
The problem is, that the eventhandler for the "add to cart" button is called after the page_load. That means that the old items are bound.
The solution would be to manually force a .DataBind() in the eventhandler.
Nevertheless, I would recommend to stay at
if(!Page.IsPostBack)
{
// bind cart
}
in the Page_Load to avoid multiple binds.
Ok, since my Add to cart button is just a hyperlink and the user control checks for a flag in the querystring to fire off a method that moves the item (it gets the item # in the querystring as well to move), I just did a redirect (Response.Redirect(Request.Path);) after the move to cart to the parent .aspx page. That way it will hit the page load for both the cart.aspx and myusercontrol.ascx and rebind the grids.
精彩评论