Prevent a PostBack from showing up in the History - asp.net

I have a page where I submit some data, and return to the original form with a "Save Successful" message. However, the user would like the ability to return to the previous page they were at (which contains search results) by clicking the browser's "Back" button. However, due to the postback, when they click the "Back" button they do not go to the previous page ,they simply go to the same page (but at its previous state). I read that enabling SmartNavigation will take care of this issue (postbacks appearing in the history) however, it has been deprecated. What's the "new" best practice?
*Edit - I added a ScriptManager control, and wrapped the buttons in an UpdatePanel, however now I'm receiving the following error:
Type 'System.Web.UI.UpdatePanel' does not have a public property named 'Button'
Am I missing a reference?
*Disregard the above edit, I simply forgot to add the < ContentTemplate > section to the UpdatePanel :P

If you put your "Save" button in an UpdatePanel, the postback will not show in the users history.

I would avoid if possible. A better solution would be to have a button that just returns them to their search results on the "Save Successful" screen.
The problem with the ajaxy saving and such is that you violate the "Back" rules that users expect. This user might want the Back button to go back to the Search page, but other users might expect that clicking Back would return them to the Add/Update page. So if another user tries to update something, clicks save, and then "woops, i forgot something on the update", they'll click back, and now they're at search results, instead of the expected Update page.

Related

Button in CustomControl added in OnClick doesn't postback until second click

I have the following scenario:
UserControlA contains a <asp:Button id="bSomeid" onClick="AddItem" /> with some code to an item to a shopping basket in AddItem.
UserControlB contains some LinkButton's that dynamically add a selection of UserControlA to the page in the OnClick event.
This is all done in an UpdatePanel. It is a little more complicated but I have pruned the information to what I believe is causing the problem, I will add more information if necessary.
The problem I have is that it takes 2 clicks for the AddItem event to trigger after I have added the items to the page after clicking the LinkButton.
I understand why this is happening - it is to late in the page cycle to register events for the next post back in the onclick - but can anyone think of a way around this? Can I force an event to be triggered on the next postback? I have tried to think of a way to run my code in page_load but I requuire access to the sender in the onClick.
Using .NET 4.0.
EDIT
I managed to find a way to get the link button sending the request in the Page_Load (using Request.Form["__EVENTTARGET"];) so I moved my code to the Page_load event. It still requires 2 clicks so I am assuming it isn't something to do with the onClick being registered to late.
Are there any other general things to check that could cause a button to require 2 clicks to post an event properly?
If your suspicion about being late in page life cycle is true then you can try using ScriptManager.RegisterAsyncPostBackControl method to register dynamically added controls in the link button click - considering that your button is within user control, you need to add public method into UserControlA that would actually register the button bSomeid1 and link button click from UserControlB would actually call the A control's method.
EDIT :
Another cause for button click not happening can be that button being dynamic control is not added in the page hierarchy when post-back happens (or it gets added very late in the page life cycle when the post back data is already processed). A really full-proof solution should add dynamic controls back to the page hierarchy in page_load it-self (and strictly maintaining same controls ids within hierarchy). If that's not possible then you can sniff the request (Request.Form) to detect the post-back.
In your case, you should ascertain if the button is indeed causing the post-back on each click. If yes, what is the POST data (Request.Form) for the first request - what is the __EVENTTARGET value on the first click (and post-back)? That should start your trouble-shooting.
On the other hand, a simple work-around could be to use html anchor element (you can still use link button) and have a javascript handler in the click event that would set some hidden variable and then submit the form (you can simulate the click on hidden button to trigger ASP.NET client side submit pipeline) . Now the hidden variable value can be used on the post-back to determine which link button has been clicked.
"Are there any other general things to check that could cause a button to require 2 clicks to post an event properly?"
Does it require two clicks on the control, or does it take accept a single click elsewhere on the screen, and then fire first time with a single click on the control?
I have my own (similar) issue with the Updatepanel where the first (expected) trigger does not fire and it seems that a single click elsewhere, and then the subsequent triggers fires first time (which totals 2 clicks)
[edit] Since you are working on this ATM, it may help me as well. Do you have a textbox with a trigger event on it? I do, and if I leave this blank (so that it does not fire) then there is no need for a second click.

listview itemcommand not firing after back button

I have page that uses a multiview. Each view contains a separate user control. One of these user controls has a list view with an image button that causes the loading of a different view in the multiview. All is fine up until this point. When the user hits the back button, they are taken back to the user control that contains the list view. The user then clicks on another image button to view different data and it returns to the detail user control using the same data as before. While debugging, I have seen that the item command event does not fire after hitting the back button.
I have tried replacing the multiview and putting each user control into separate panels. This did not change the outcome at all.
I have tried setting a cookie that expires 5 seconds after page load. When the user continues to the next page, then clicks back (and it has been longer than 5 seconds), I force the form to submit again. This loads the next control again instead of reloading the page.
I have tried setting the cacheability to no cache. This causes a "page expired" message and the user has to refresh the page. This is ugly for the user and definitely takes away from the user experience.
I am looking for the cleanest way for a user to click back and have the page reloaded so that the item command event fires correctly again.
The reason is that Back doesn't affect the Page Life Cycle. It's definitely because the page is cached and cached page doesn't execute on server. You can try this code to get rid of this issue.
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
It took a lot of research to find this answer, so hopefully other people stumble upon this question and find my answer. I was astonished that I was actually able to find this. Ok, enough gloating.
Because the page does not postback when the user clicks the back button, the events are not fired correctly causing problems with the next page. What has to happen is you need to be able to handle the browser's navigation buttons (i.e. back and forward). To do this you have to set EnableHistory to true within the script manager and handle the Navigate event from the script manager. You can then reload the controls using the information you save in the state object.
I used these articles from Dino Esposito on DotNetSlackers.com as a reference. Server Side History Management and Client Side History Management

Validation issue

In a program I have 1 multiview with several views into it (where views are menu options).
The problem is my error message already shows up from the moment I click on the menu option (a certain view).
After input it disapears and when I leave it empty for the next input, the error message comes back.
So in other words it works fine, but when I click on the menu that error message should not be there (when the page loads/shows for the 1st time).
EDIT: a (IsPostBack) within a function seemed to have solved the issue.
IsPostBack can be so confusing at time :>
I would recommend wrapping all of your controls with a unique ValidationGroup per view. The problem is that all validation is firing on postback, so any validation control (hidden or not) that is invalidated will show the message as long as it is within the current view. You will also need to add the same ValidationGroup to the buttons that are navigating between the Views. I would also bet that you are not checking if Page.IsValid() is true before navigating to the next view. If you do, you will notice that you will not be able to navigate away from the current view because the validators in the other views are invalidated.
You can also set CausesValidation on any button to False to prevent any validation from firing.

Button click method runs after page loads, which means page doesn't update, how can I solve this?

I have a button, which updates a value in the database. This value is used to determine what to draw on the page. Because of the page lifecycle though, the page redraws before the button click method is executed, meaning that any changes are not reflected until the page is reloaded again.
What's the best solution for this?
To clarify:
Page has a piece of text, that says "I like cats" if the database value is 1
button 'I hate cats' is pressed, which sets the database value to 0
the page reloads, but still says "I like cats"
the button click event is handled, and database value becomes 0
If the page is refreshed/reloaded, it now correctly says "I hate cats"
It should update when the button is clicked though.
you can use the page prerender event. this fire after the control event in the page lifecycle.
You can solve your problem by below code, use this end of button_click block
for ASPX:(C#)
Response.Redirect(HttpContext.Current.Request.Path);
Where are you querying the database? One easy option would just be to use Response.Redirect back to the page.
I think you could just get the result from the button method and just update a label/literal.
One possible solution is to execute a javascript function that updates the database (using ajax) when the OnClientClick event for the button is raised. In this case, when the page reaches the Page_Load event you will be able to render the appropriate content, as the postback happens after the script is executed. It should work fine if the database update takes a relatively small amount of time.
One curiosity, are using any anything like Page.IsPostBack { do something }.. If so, could you check if your update UI code is outside this check.

How can controls (buttons) be set in a ModalPopup Extender panel that do NOT close the panel?

Here's the situation.
When a user is editing a given piece of data, they're allowed to add messages/comments. These are stored as child records in a SQL database. Clicking on the Add Message button brings up a panel (pnlMessage) courtesy of the AJAX ModalPopup Extender. This takes some input and, when the "Send Message" button in the panel is clicked (I learned the hard way to NOT make that the 'OkButton' property), the message is stored in the database and an email is sent to the intended recipients. No problem there.
However, I need to be able to allow the user to add new email addresses (so long as they are registered in our database). I have another ModalPopup / panel combo (pnlSearch) that's tied to a button on the previous panel (pnlMessage).
The user is supposed to be able to add an email or click on a search button to populate a list to choose from.
The pop-up panel (pnlSearch) comes up just fine, but clicking the "Lookup" button (which instigates the search and returns a collection of records that the user is supposed to pick from) closes the panel.
Previously, I ran into the problem of having the Button.Click event never firing when I put the Button into the "OkControlID" property (the CancelControlID works fine since I don't want to do anything). Removing the "OkControlID=Button" line allowed it to work perfectly with the Button.Click event firing as expected.
So now I have the Search panel with a button for "OK" and a button for "Search" - but the panel should stay up and visible after the Search.Click does it's thing. Am I missing some property that basically says "don't close the panel when this button is clicked"? Of course, if I bring up the panel again in the same session, the results from the previous effort are there (the search results).
I'm trying to avoid having to go to javascript as there isn't much, if any, of that experience available to support this.
Help!
Thanks in advance.
You can put the Search panel and the Search button inside of an UpdatePanel. Anything inside of the UpdatePanel will be able to post back without closing the popup. Be sure not to put the buttton that is supposed to close the popup inside of the UpdatePanel.

Resources