ASP.NET does not provide a way to do this directly.
On the other hand, there are several methods to avoid duplication of view:
Redirection after sending. This is the worst. Even if this avoids duplication of view, it is unacceptable in a modern web application from the point of view of users.
Track messages in the form per session . When a user submits a form for the first time, remember this in the session. If another view occurs, try to determine whether it should be discarded or not (in some cases it should not be, for example, if I edit my answer to StackOverflow once, I could do it twice if I need to).
Disable JavaScript submission after the first submission. This allows in some cases to avoid a situation where the user double-clicks the submit button or clicks on it for the first time, waits and considers that the form has not been submitted, thus, clicking the second time. Of course, do not rely on this: JavaScript can be disabled, it will work with a double click, but not with the F5 update, and in all cases this method is not completely reliable.
As an illustration, we will try to implement the second.
Say we have a comment field this.textBoxComment that allows users to add a new comment to a blog page. The presentation is as follows:
private void Page_Load(object sender, System.EventArgs e) { if (this.IsPostBack) { string comment = this.ValidateCommentInput(this.textBoxComment.Text); if (comment != null) { this.databaseContext.AddComment(comment); } } }
If the user double-clicks, the comment will be sent twice.
Now add session tracking:
private void Page_Load(object sender, System.EventArgs e) { if (this.IsPostBack) { if (this.Session["commentAdded"] == null) { string comment = this.ValidateCommentInput(this.textBoxComment.Text); if (comment != null) { this.databaseContext.AddComment(comment); this.Session.Add("commentAdded", true); } } else {
In this case, the user will be able to post a comment only once. All other comments will be automatically deleted.
The problem is that the user may want to add comments to several blog posts. We have two possible ways of doing this. It is easy to reset the session variable for each request without postback, but this will allow the user to send a message on one page, load another page, and not delete the update on the first, thereby sending a comment twice, but not being able to add a comment on the second page.
private void Page_Load(object sender, System.EventArgs e) { if (this.IsPostBack) { if (this.Session["commentAdded"] == null) { string comment = this.ValidateCommentInput(this.textBoxComment.Text); if (comment != null) { this.databaseContext.AddComment(comment); this.Session.Add("commentAdded", true); } } else {
More difficult is to track in a session the list of pages on which the comment was sent.
private void Page_Load(object sender, System.EventArgs e) { List<string> commentsTrack = this.Session["commentAdded"] as List<string>; string blogPostId = this.ValidatePostId(this.Request.QueryString["id"]); if (blogPostId != null) { if (this.IsPostBack) { this.AddComment(commentsTrack); } else { if (commentsTrack != null && commentsTrack.Contains(blogPostId)) { commentsTrack.Remove(blogPostId); } } } } private void AddComment(List<string> commentsTrack) { if (commentsTrack == null || !commentsTrack.Contains(blogPostId)) { string comment = this.ValidateCommentInput(this.textBoxComment.Text); if (comment != null) { this.databaseContext.AddComment(comment); if (commentsTrack == null) { commentsTrack = new List<string>(); } commentsTrack.Add(blogPostId); this.Session["commentAdded"] = commentsTrack; } } else {