I have gridview with Custom paging. I have edit option in the gridview. when user is moving to next page, i want to show an alert message, (you have unsaved data on the page, click ok to move to next page or cancel to stay on the page.) How do i automatically stop the next button functionality when cancel button is clicked?
-
This may help you: [http://stackoverflow.com/questions/6457982/can-i-stop-a-post-back-by-using-javascripts-confirm](http://stackoverflow.com/questions/6457982/can-i-stop-a-post-back-by-using-javascripts-confirm) – j.f. Jan 28 '15 at 14:08
1 Answers
If you are trying to prevent a postback just to display an alert popup, that's not really a good reason to prevent the postback. If you are trying to prevent a postback to save on network traffic and server processing that's a different story.
But in the ASP.NET world on a page change you should be handling GridView1_PageIndexChanging
. In that event check if the gridview is in edit mode: GridView1.EditIndex > -1
and if so set e.Cancel=true
to prevent a page change. This will execute a Full postback but will leave the Gridview in edit mode and keep entered data intact.
Then, to inform the user after the postback completes, you can fill some <asp:Label />
to inform the user or you can maybe fill an <asp:HiddenField />
with a message that is displayed after page load if there is text in the field. Really it depends on what you're trying to accomplish.
If you want to reduce network traffic consider wrapping the gridview in an UpdatePanel. But if the gridview is say one of a few controls on the page it may not be worth the overhead of the UpdatePanel.

- 4,531
- 1
- 15
- 23