force save on detailsview/formview



When a user is using a Detailsview or Formview in Edit mode, and tries to leave the current page, I want to trap this and force any changes to be saved, just as if the user had clicked on the Update LinkButton. How can I do this?

  • Further to the answers below, I discovered a series of articles on this topic:

    dsteele22 августа 2009, 14:53

1 ответов


You can't do this reliably. Consider the scenario if their computer crashes altogether, or they kick the power cord out. There's so many ways that a user can leave a page.

However, you could put in a few safeguards. On any link on the page you could hook up events to do the save before they left. You could also try doing a save after every change in control focus perhaps.

Another thing you can do is hook into the window.onbeforeunload event and give them a confirmation message asking if they're sure they want to leave the page, like SO does when you have an unsaved answer. But there's nothing you can do with this event to force a save.

  • Thanks to both of you. I’m aware that I can’t cover all possible situations, but the most likely in my app is that a user just forgets to click ‘Save’ when he moves to another page.

    dsteele22 августа 2009, 00:32
  • Мы используем третий метод, перехватываем событие onbeforeunload и подтверждаем, что пользователь хочет уйти без сохранения (мы используем флаг dirty, чтобы узнать, когда есть несохраненные данные, поэтому мы не всегда показываем подтверждающее сообщение).

    mbillard22 августа 2009, 00:07