How do I reset Page life cycle from code behind? - c#

I have a web page that is meant to be a product view for a computer. I have a div next to the product photo that contains the product information. The div is runat="server" and when the user selects a 'customize' button within the div, I have C# in the code behind button event handler that erases the current product information in the div and replaces it with drop down menus for the customization options.
I want to have a "Cancel Customization" button that allows the user to stop customizing and go back to the original product view page. I have tried using Response.Redirect("CurrentPageUrl"); but because I am redirecting to the page I am currently on, I guess it's a postback and not a full life cycle reset. The result is that the page reloads with the div just empty. What I really want to do is reset the page life cycle somehow, so it is as if the 'Customize' button was never clicked and the original page code, including the product info, on the server is completely reinitialized. I have looked around for how to do this but everywhere just seems to recommend Response.Redirect() which as I have said does not produce the desired behavior.
I realize I could just reinsert all of the original product information from the original page but this seems like a lot of code and I figure there must be a way to just re-initialize the Page?
Summary:
Page initializes with div filled with Product Info.
Customize button is clicked, button handler in C# code behind removes contents of div and replaces it with customization options.
desired behavior: 'Cancel customization' button is clicked, page is reloaded so product info is now back in div.
current behavior: 'Cancel customization' button is clicked, Response.Redirect("CurrentPageUrl") is called, page posts back and div is now empty.
I will be checking this post very often for the first few days so please don't hesitate to ask for more info I'll get back to you quickly. I apologize in advance if this question has already been asked, I did spend quite a bit of time looking for answers on S.O before posting this and as I said all I could find were recommendations for Response.Redirect() which is not what I'm looking for. I am developing the site using VS2019 ASP.NET framework web forms if that makes a difference.
Thanks for any answers!

Related

Browser back button fails - ONLY IF postback has been performed on previous page

I'll keep this simple. I have two pages:
List page
Detail page
Quick info on the list page:
A grid to show list of items with a "view" hyperlink each pointing to the detail page (e.g. detail.aspx?id=1)
A number of filters and a search button
By default the list shows all items without any filtering
Scenario 1 (OK):
Without any filtering, click on any of the view links
This will take me to the detail page of the selected item
Click on the browser back button, the list page is shown (OK)
Scenario 2 (Not OK):
Do a search filter on the list page, this triggers a postback with filtered items
Click on one of the view links
This will take me to the detail page of the selected item
Click on the browser back button, ERROR PAGE!
Error info:
IE: Webpage has expired
Chrome: ERR_CACHE_MISS
Firefox: Document expired
Any idea why scenario 2 fails to bring up the list page? I don't need the filters to be retained when going back, I just need the list page displayed straight away without the error.
Thanks in advance
Try to clear the cache. Here is a similar discussion.
But what I did was to disable the back button with a line of Javascript to prevent these kind of problems
<script type="text/javascript">
history.forward();
</script>
And I inserted a "Back" or "Return" button into the second page which will be used to go back to the initial page.
I hope this is convenient for you.

C# asp.net Maintain tab index after asynchronous postback ontextchanged

Ok, so I've researched this to the end of the earth and can't seem to find a solution that works.
I have a C# application, which is basically a web form made up of radio button lists and text boxes in an update panel. The form has multiple 'sections', each of which the user submits when completed. Each control performs a postback. This is because if the user modifies a section after completing it, the 'section saved' label needs to disappear.
This all works well, except the postbacks lose the tab order of the controls. I have found code examples that save the last control that had focus, which works well for the radio button lists, but because the text boxes post back when a user tabs to the next control (not modifies the text), it doesn't select the next control. The user has to hit tab again and it jumps to the third text box, not the second because technically, the second text box is what had focus after the initial postback. I hope this makes sense.
Any ideas? I can post code if required.
I should probably also add that this page is within a frame of our community portal.

Go back to to previous page ASP.NET

Im having an issue of going back to the previous page. The page i want to go back to had a few radio buttons which you had to select, after this you went to the next page which is the current page which then you can select certain things BUT I want to be able to go back to the previous page and the original selections for that page still be selected.
Anyway i could do this if so how?????
You can do a real basic back button with the help of JavaScript.
window.history.go(-1);
which will take you back to the previous page.
By default in ASP.NET state of controls is stored in ViewState, so it should be the same as user left them.
It is probably some view-state issue.
Go to your codebehind and check PageLoad method. If you are creating or setting radios in PageLoad, you do not want to re-init them every post-back.
if (!IsPostBack){
// your init here
}

getting some problem with gridview paging

I have two pages.
In one page I have placed a GridView. In the grid I am using two hyperlinks for select and edit. When I click for the edit page I am redirected to another page. When I have finished editing the record it goes back to the first page.
The problem is when I was on the 2nd or 3rd page of the GridView and then edited, it went back to first page, not the 2nd or 3rd page I was on.
I want the same page after I go back to the page I left. How can I go about this?
The problem is that the webpage needs to know what page to send you to. The way I do this is to pass a querystring parameter when I change pages.
So in the URL to the edit page you want to have the URL say (for example) http://server/edit.aspx?value=number&returnPage=2 and then when you go back to the grid page you'll pass the page value like http://server/grid.aspx?page=2
This way you'll always know what page to tell the GridView to goto. And it should be pretty easy to add in depending on your level of comfort with the Params object.
Response.Redirect("einward.aspx");
you have to redirect to the page, wtever page u want.

Changing page contents on postback

I have a survey, where you have to click through several pages with questions. I use a "Next" and a "Previous" button for doing this. I use a session for keeping tabs on my position. However, this is a problem.
I use the button_click event to increment the page counter, but since this fires after the page_load event, nothing happens on the first click, and for every click thereafter everything is one page behind because the questions are rendered before the counter is incremented.
Is there any way to solve this without using the query-string?
Comment response 10:15:
My understanding is that the following happens:
The first page loads and the counter is not set. The counter is set to 1. The first page is displayed.
The user clicks the "Next" button, firing a postback.
The page loads, displaying the same set of questions, because the counter is still on 1.
The buttons Click-event is run, incrementing the counter. However, the page is still displaying the old questions.
After this everything is one page behind because the questions are rendered before the click-event is fired, incrementing the page counter.
I'm probably not seeing something obvious here :|
Why don't you use the out-of-the-box Wizard control? This is what it is designed for. They have solved all of this issues that you are likely to have and it is entirely customizeable.
Scott Gu's blog
MSDN Walkthrough
Like Pavel said in the comment posting some code to clarify the question might help in seeing where you could have gone awry. There could be several things amiss, first check to make sure your logic is sound in your code. Go through step by step and see if it doing what you are wanting. Setting up break points is also a good idea to find the issue. It could be just a variable out of place (this has happened to me many of times and caused a lot of hair pulling).
I got past it by using the LoadComplete event to set all the "dynamic" text and controls. Since the LoadComplete event of the Page object fires after the postback-events of the controls, this works as I want it to.

Categories