asp.net session in usercontrol empty until page refresh - asp.net

I'm creating some sort of "wishlist" where users can click on any item in a repeater (clicking on a button).
When a users clicks the button I add that item to a session("wishlist"). The session contains a list of strings that are shown in a wishlist.
To render the wishlist on several pages I'm using a user control that contains a repeater that just loops over all items in that same session variable.
This works great, except for when I click on an item button "add to list" in the repeater, the user control does not show this newly added item in the wishlist (repeater looping over the session) until I manually refresh the page.
The button "add to list" in the repeater is triggerting a postback so I would think that the user control is being rendered again from the ground up.
Any ideas what's wrong?

Related

Click event of Link button inside a gridview of user control not working when the control is added dynamically in a web page asp.net?

On clciking a button in the page , I am adding an user control in to a place holder of the page.
The user control has a gridview which has a link button as template field and has a click event associated with it.
When I click the link button inside the gridview , the link button click event is not fired and also the user control disappears in the page.
Please suggest a solution
--You need to recreate all your dynamic controls at the early enough in the page lifecycle (page_load/page_init), so the asp.net will detect events and attach them.
try this knowledge post
http://support.microsoft.com/kb/317794
Why don't events of dynamically added user control fire on postback?
You need to recreate your dynamic controls on every postback - remember a new class instance of your page is created every postback, so any dynamic controls you created the previous time will need to be recreated.
see this article
and this one

Previous page should remain in same state when back button of browser is click

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

Why does drop down list in ListView in user control cause full page postback in page-level update panel?

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.

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

Passing values between tabs in AJAX tabcontainer in ASP.net

I have a ASP.net page with couple of tabpanels. When a user hits submit button when he is on the First tab I am showing the user the second tab control. Ontabindexchanged I am dynamically creating a usercontrol and passing some values to the control.
Now when the user is in the 2nd tab and if he navigates to the first tab, I need to pass some values to the first tab.
At the tabcontainer level how do I pass values between tabs?
I think what your problem is, you need to maintain viewstate of your dynamically created controls, otherwise there is no problem to pass values between tabs. like..
tab1TextBox1.text=tab2Textbox2.text (Since your controls are in AJAX tab panel, no need to find control from tab panel)
For Maintaining viewstate of your dynamically created user control, you need to create your control in page_init() event.

Resources