Last Action Event get Fired on back Click - c#

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!

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!

Search Button on Customized Gridview Takes Two Clicks to Fire Event

I have a customized Grid control that is inherited from Gridview that has search and pagination. Everything works great except this one little thing. Whenever I use the pager to go to the next page, then I use the search, the event that handles the search does not fire on the first click, it takes a second click to get it to fire. Any ideas?
The code for the control is here:
Grid.cs
Important Note
I am aware that it is frowned upon to post large blocks of code... But, the entire control's code is posted to give the whole picture of how it is built. The control itself is a bit on the complex side as it performs searching, sorting, and pagination all server-side; and this code is a completely custom control that just INHERITS the GridView.
I AM NOT looking for someone to write a fix for me, just an idea of why this one situation may be happening!
With that said, to break things down a little more with the code... The search form (text box and buttons) are created dynamically and added in the CreateChildControls method. The search form works perfectly when the Grid is initially loaded, but after using the pagination, the first click of the Search button does not fire the method assigned to the Search button's command event, but the second click does. It seems as though something in the postback is not recognizing the button's command event has been triggered...
A trace of the calls to the methods show:
1st Click - Everything from ViewState is loaded, no postback events are called.
2nd Click - Everything from ViewState is loaded, postback events called.
I'm looking for ideas on where to go from here, as I've been trying everything I can think of page life-cycle wise to see if I can get this functioning properly. The only thing that has worked is setting EnableEventValidation to false on the page that implements the Grid control, and performing the "initial" data bind on every "Page_Load" (not just "if !IsPostback"). But, for obvious reasons, that is not an acceptable solution.
Found the issue... Posting here for anyone who might make a similar mistake.
In CreateChildControls(), I was executing the base before adding the search form to the control. A simple switch around to calling the base after adding the search form, and getting rid of the condition "if (this.HasControls())" resolved it. From what I can see, calling the base after adding any child controls allows those child controls to have their events/handlers properly registered.

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
}

To provide confirmation message in web page only if any change is done on the page

I want to show a confirmation message on click of cancel button and then redirect to another page. But the confirmation message should come only if any change has been done on the page.
Please can anyone help on this. Is it possible to achieve this at client side or at server side without comparing data ?
Thanks in advance.
I'm guessing you could have a scenario where:
A form is populated with values and each form item that you are tracking for changes also has a corresponding hidden field.
If the user clicks submit = great, but if the user clicks cancel you want to loop through each form item, comparing it to it's original value stored in the hidden field. If a change is detected you want to show a pop-up window informing the user that if they cancel their changes will lost - or something like that.
You can do all this on the client. JQuery, or Knockout (which I would use) or Backbone (I don't know this one tbh) should all provide a neat way to achieve this.
If you can't have hidden fields you might want to send the new values to the server in an Ajax post, get the server to pull the original values, compare and send back an appropriate response to the client. I'm sure there are other ways too*.
edit: like pop the original values in an array via Javascript, then compare them. Anyway, I'm sure you get the idea :)
This could be accomplished using jquery you would need to compare data since you want the message to come up when the cancel button is clicked.
jquery reference

Displaying error message without postback?

I got this an asp.net 3.5 page that and I have few tabs (telerik tabs control with pageview) and I have panel on the top of the page and inside that has a label control displaying error message if it's any.
At the moment, if I want to display this, at the end of the event clicked button for instance, I have to do a custom URL redirection class to itself passing error message and display the error. BUT the problem with this approach let say you are working on 4th tabs and you click save button inside this then you loose the state of series of control (what's is being choosed, selected, typed etc). The page refreshed and displayed at the first tab again.
I want to display the error and at the same time know the state is for every control on the 4th tabs and automatically goes to 4th tabs. BTW ... this validation is done through server level.
I am appreciated your feedback/suggestion.
Thanks
I see several approaches here:
Make an ajax call with data to validate and show error message basing on its result.
Recover your control state after post back, you will need to write some client logic that will analyze url for some parameter after "#", e.g. http://my-site.com/tabs.aspx#4. Your script will know that you need 4-th tab.
You can try to recover your control state on the server, but I don't know how it is possible with telerick tab control.

Categories