.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
一个通用的Confirmation Page类

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

Confirmation Page

 

1.      Introduction

In New or Edit page, users might press the Close button by mistake and lost the content that they have input. To avoid this mistaken action, application should prompt users before the page is closed. This page will not be 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 the user to confirm navigating away from the current page”.

 

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


namespace Proloq.ProloqWeb
{
 /// <summary>
 /// Will 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 will 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 will lose any unsaved content";

 

    this.SaveButton.Attributes.Add("OnClick",ConfirmationPage.ByPassFuncationName);

}

 

 

 

 

 

 

 

 

 

 

 

SaveButton is the button which you do not want to issue the confirmation. Add other buttons you want here.

 

And in PageLoad, you 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 at server, you should put following code for them. Take Cancel button in Product Edit page for example:

<IMG style="CURSOR: hand" onclick="javascript:bypassCheck();GoBack();" src="<%=Image1URLBase%>/btn_cancel_g.gif" border="0">

 

 

 

 

bypassCheck() function is to clear the confirmation for the page.




相关文章

相关软件