0

I want to call function which is affiliated to click event when page get changed or refresh in the asp.net c#. Is it possible. If possible can you please help me out.

Thank you.

Eg: IF you are on the "abc.aspx" page and you jumped to "bcd.aspx" page so, in between this time I want to call the function which affiliated to the abc.aspx page's click event when you jump to other page.

It is just like automatic saving the data.

  • To call a button submit action you can do it like this: btnName_Click(Submit, null) which this is the onClick event in the submit button – Brad May 18 '18 at 19:24
  • For your part about when a page gets change or refresh do you have more details on what change means? Data in a form changes? – Brad May 18 '18 at 19:24
  • Thank you for your answer but, i want to know that when you jump to another page without hitting the save button then web form won't going to save automatically. – martin christie May 18 '18 at 19:26
  • When the user is about to leave *abc.aspx*, you have to ask them if they want to save their changes, then save it. Afterwards, they can go to the other page. This is how web apps work. You cannot just save automatically because maybe the user does not want to save the changes. However, if you want to save the changes automatically, you have to do this using AJAX. Otherwise, by the time the user is in *bcd.aspx*, it is impossible to save data from *abc.aspx* because that data is already lost. – CodingYoshi May 18 '18 at 19:38
  • Thank you CodingYoshi. I appreciate that. I got the view now. – martin christie May 18 '18 at 19:45

1 Answers1

0

That is not possible and here is why. You need to understand how the web, or web applications in this case, work.

Imagine a user is in Canada and they request page abc.aspx from a server in Australia. The server returns this abc.aspx's html to the browser. The user enters some data and then, without saving, they request another page named bcd.aspx (you call this "jump" but it is actually a request). Now the request arrives to your ASP.NET C# code in Australia (the server). The server has no idea what the user did with the previous page. Actually the server does not even remember the user asking for the previous abc.aspx page, unless you have sessions or something but even in that case, you have no idea what the user did with the previous page. Therefore, it is impossible to save the data because there is no data. It is just another request for another page.

So there is no way to save the data?

Good news is that yes there is but you have to write code for it and you have to do it before the user leaves the abc.aspx page. You need to write JavaScript code which will be executed on the browser side on the user's computer. When the user is about to leave the abc.aspx page, you either ask the user if they want to save their changes or your JavaScript code will call your server (C# Code) and send the data to you so you can save it. Not only that, it will have to do this using AJAX. So like this:

  1. User is about to leave (and optionally JavaScript asks the user if they want to save their changes. It would be weird if you do not ask and just save because maybe the user does not want to save)
  2. JavaScript makes AJAX call to the server and sends the data to save
  3. The server saves the data and sends a SUCCESS response
  4. JavaScript receives the response and then request the next page.
  5. Server returns the next page
  6. Browser shows the next page.

If you want to take that route of saving before leaving the page, please see this thread.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64