A generic Confirmation page class

Source: Internet
Author: User
Tags bool
Confirmation Page


1. Introduction

In New or Edit page, the users might press "close" button by mistake and lost is the content that they have input. To avoid this mistaken action, application should prompt users before the page is closed. This page is not is closed unless users confirm that.



This document gives the implementation details for how the confirmation Page works. It can be used in:

? New Page

? Edit Page

? Any other pages need confirmation before close



2. Solution

2.1 Confirmationpage Class

Confirmationpage class is available in the root of Xxxweb project. This is a class derived from pagebase.

This class implements the function of ' cause ' to confirm navigating away the ' current page '.



Using System;
Using System.IO;
Using System.Text;
Using System.Text.RegularExpressions;
Using System.Web.UI;


Namespace Proloq.proloqweb
{
<summary>
Would cause the user to confirm navigating away from the current page. This behavior can be overriden.
</summary>
public class ConfirmationPage:Proloq.ProloqWeb.PageBase
{
Public Confirmationpage ()
{
}

private bool _isinedit = false;
public bool Isinedit
{
get {return this._isinedit;}
set {This._isinedit = value;}
}

private string _message = "You'll lose any non-saved text";
public string Message
{
Get{return _message;}
Set{_message = value;}
}

protected override void OnPreRender (EventArgs e)
{
If we are in edit mode, register the script
if (Isinedit)
{
Page.registerclientscriptblock ("Confirmationbeforeleaving", String. Format ("{0}{1}{2}", Scriptstart,message,scriptend));
}
Base. OnPreRender (e);
}



Const string Scriptstart = "<script language=\" javascript\ ">g_blncheckunload = true;function RunOnBeforeUnload () {if (g_blncheckunload) {window.event.returnValue = ' ";
Const string scriptend = "';}} function Bypasscheck () {g_blncheckunload = false;} </script> ";

public static readonly String bypassfuncationname = "Bypasscheck ()";


protected override void Render (HtmlTextWriter writer)
{

If we are in edit mode, wire up the onbeforeunload Event
if (Isinedit)
{
TextWriter tempwriter = new StringWriter ();
Base. Render (New HtmlTextWriter (Tempwriter));
Writer. Write (Regex.Replace (tempwriter.tostring), "<body", "<body onbeforeunload=\" runonbeforeunload () \ "", Regexoptions.ignorecase));
}
Else
{
Base. Render (writer);
}
}
}
}




2.2 Implement in Codeback

2.2.1 Inheritance

The pages going to use confirmationpage should derived from Confirmationpage class. Take Product Edit page for example:

public class Product_edit:confirmationpage





2.2.2 in Codebehind

Put a private function like bellow in codebehind:

private void Setconfirmation ()

{

Confirmationpage confirmpage = (confirmationpage) this. Page;

Confirmpage.isinedit = true;

Confirmpage.message = "You'll lose any unsaved content";



This. SAVEBUTTON.ATTRIBUTES.ADD ("OnClick", confirmationpage.bypassfuncationname);

}























The Savebutton is the button which you don't want to issue the confirmation. Add other buttons your want here.



And in Pageload, your should use this setconfirmation () function:

private void Page_Load (object sender, System.EventArgs e)

{

...

Setconfirmation ();

...

}













2.2.3 in. aspx file

If There are some buttons are not run in server, you should put following code for them. Take Cancel button in the Product Edit page for example:

/btn_cancel_g.gif "border=" 0 ">









The Bypasscheck () function is to clear the confirmation for the page.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.