Go back to to previous page ASP.NET - c#

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
}

Related

How do I reset Page life cycle from code behind?

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!

Last Action Event get Fired on back Click

I have a Form which contains :
Category TextBox
Button to Save the TextBox value in database.
The code works fine, all textbox value get Saved successfully on Save Button Click.
Problem:
After Saving I redirect to same page, and now here when I click on some link suppose 'Google.Com' and then if I click back button the browser redirect me to previous page on my application. on Click on Back Button the Postback value is found to be True, and again Save Button Event gets Fired and again textbox value gets saved in database.
I don't want the event must get fired on back click.
What should I do?
Please help me , I m getting issue and not getting solution on it.
What you need to do is to implement a Post/Redirect/Get (PRG) pattern in your app. The basic rule is that your POST should not return a page (or a view); instead, it should return just a redirect request that will GET the page/view to show.
While you'll find a lot more information about PRG in relation to ASP.NET MVC, I'm assuming your working with web forms - so there's not much in terms of examples out there that I'm familiar with.
But the concept is pretty straight-forward. After your POST, just return a Response.Redirect back to the page that you want to display. It may seem like duplicate work, but you'll save yourself the problems that you're describing.
Hopefully this helps!

Page state is lost while refreshing the page

In my webpage, there is a repeater control which holds an image and its description. After the page is been loaded, the user can reorder the images(Jquery is used for reordering divs).
After the reordering, if the user refresh the page or click any button, the page will go back to the previous state before reordering.
How can I prevent this.
The repeater control is in .aspx page.
Thank you in advance.
If the page is refreshed there's nothing really you can do. The browser will send the same request you sent before.
Because the sorting is done client side, you should save it states somewhere and then you can request again the page to the server.
So for instance, let say that you order your repeater by the "CreatedDate" column. Once ordered, you can save the column name of the ordering in a hidden field. Once you click a button, the page is loaded again and you step in the page load event in the code behind.
At that point you can get the value of the hiddenfield posted, you will find that it got "CreatedDate" as a value. You can then return the datasource for your repeater already ordered by date and the user will have the same view he had before posting the page.

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.

Pager refresh issue

I am having a listview and binding it with items retrieving from database. I have used pager to navigate to various page. But problem is that suppose currently I am in page number 25 and refreshed the page by hitting F5 or clicking on the browser refresh button, it will redirect me to the page number 1. I mean to the first page. But I want after refreshing also it should be in the current page I mean in page 25. I am not getting why it is happening.
It is better to add a querystring with the current pagenumber. for eg Consider your page is a.aspx and just pass an querystring like this a.aspx?pg=1.
On the page load if(Request.QueryString["pg"]!=null) then just display the records of page 1. Suppose if you want to show at 25th page then url will be like this: a.aspx?pg=25.So if a user refreshing by F5 it will still displays the same data.If the page has undergone postback (due to button click event or dropdownlist selected index changed etc) it also displays the same record because of Querystring. You can also change the pagenumber by redirecting it to same page with another value in querystring Eg:Response.Redirect("a.aspx?pg=26").
I think this will solve your problem.
If you are programatically binding on the page load event, this will happen. You could set a session variable when the user changes page and manage this in the page load event too.

Categories