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 even 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.
Related
I am trying (and failing) to get a very simple webpart working.
button which when pressed adds list item into custom list (this works great)
list out all the items from the custom list (this also works great)
the problem is that when i display the items and add a new item i need to refresh twice to get the listing to display the item that i am trying to display. What is the correct lifecycle that i need to follow with createchildcontrols, onload etc so that when i click the button and it adds a record in that the listing will display the newly inserted item.
Thanks
This is probably a case of setting the control values or forcing databinding in OnLoad; this is before the "add item" occurs which is before the new item has been added.
I recommend using Data-Binding expressions and ensuring databinding in PreRender1. As long all the controls follow this later-databinding it should Just Work. Otherwise, manually force update/databind the appropriate controls after (or in response to) the "add item" action.
The Life Cycle is the same as for ASP.NET WebForms, and summarized for the issue:
- "OnLoad"
- PostBack Actions such as "OnClick"
- "PreRender"
1 Databinding in PreRender works - even though it initially seems like it ought not to - because ASP.Net controls will Catch-Up as required.
I have a User Control in a Master Page that calculates quantity of products in the shopping basket and writes to page something like shopping cart(2), showing that you have 2 products.
When you add a product to basket, User Control loads first and then Add method of the page executes because of the page life cycle.
So In order to see the new added product takes effect - shopping cart(3) - i need to refresh the page..
What is the best solution in order to see this after add to cart button is pressed with no refreshing the site.
Do I need to use master page's pre_render event?
Having a look at the ASP.Net page life cycle, you'll find that the LoadComplete (for pages only) and PreRender events are called after the postback handlers.
It should be sufficient to move your shopping cart calculation into the User Control's PreRender event.
Just take care of the actual order of event execution, and call your logic accordingly:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Normally the Master Page UC should subscribe to an event which is fired by the page whenever an item is added.
Best way of doing is to use the custom Event in user control.
How ever if you don't want to use that, you can use a work around. Simply create a property of user control to show count and updates its value in Add method of that page.
I have a page on which there are two drop down lists, based on the selection of those dropdown lists grid view is filled. In grid view there are imagebuttons. When user clicks on any of the image button, he/she is redirected to another page. Now when I click on the back button of the browser, I want page in the same state as it was before redirect. How can I achieve this goal??
use ASP.NET Multiview control instead of separate two pages this will create one page with two views which will effectively retain page state.
Please refer to these articles
http://www.beansoftware.com/ASP.NET-Tutorials/MultiView-Control.aspx
code project multiview
I have the following scenario. I have a page that hosts several user controls. The user controls are all surrounded by a single update panel. All user controls have a save button on them. When the save buttons are clicked the page updates the update panel as expected.
Some of the user controls contain editable list views. Whenever an action is taken on these list views, the Update Panel is NOT refreshed, but the whole page posts back. How can I get these ListViews to also refresh the Update Panel? I cannot post the code because of NDA.
There are no javascript errors on the page reported by either IE8 or Chrome.
Check the autopostback property of the dropdownlist control, if the property is set to true it tries to postback every time the selection changes.
If this does not work for you, you may wish to try implementing manual update triggers in your controls code and setting the update panel to conditional, to firmly control when you want the panel to update.
In my application, I have a list box(list of stores), Add and Remove buttons and another list box (selected stores).
I have Following requirements:
1. On click of Add button, copy selected items from the master stores list to the selected store list and clear the selection from master store list.
2. On Click of Remove button, remove the selected stores from the selected stores list.
I have added my master store list box and selected store list boxes to 2 different update panels and added triggers for each of the update panel. Things are working fine but one thing I have observed that it is taking unusually long to move selected stores from master list to selected list. I have around 5000 entries in the master list of stores.
If I remove the update panel for master store list, things are normal but I am not able to clear the selection. Am I doing something wrong? Is there a way to clear selection of listbox outside the update panel.
It sounds like your using UpdatePanels to move ListItems between 2 ListBox controls. This creates a overhead as each time you trigger the 'add' event it has to postback and render the UpdatePanel again (including the viewstate).
Have you looked into using jQuery to move your list items between your ListBox controls thus all the moving between boxes is handled by the browser. This will speed up the experience for the user?
You should also be able to use jQuery to clear the selection. If you can provide me a little snippet of your markup I can help you get it written up (or shoot me a message).
Brian =)