`
feipigwang
  • 浏览: 743609 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

WebConfirm控件(转自http://www.evget.com/view/article/viewArticle.asp?article=961)

 
阅读更多

/* * 在看这个控件代码之前,先要熟悉以下内容:
* 0.ViewState机制和作用。
* 1. 事件机制。
* 2. 回发机制:在非窗体控件中保持客户端更改,ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpguide/html/cpconpersistingclient-sidechangesinnon-formcontrol.htm
* 3. IPostBackDataHandler的作用及实现。
* 该控件是Asp.Net服务端控件。弹出Confirm,确认提交功能。
* 当点击按钮时,弹出confirm,提示确认操作,如:用于确认删除。
* 点击“是”后,提交触发CliekTrue事件,如:可以在该事件内写要执行删除的代码。
*
* 附:调用代码
*
*
* 另外:不把EventDataArgs和EventDataHandler写在名字空间内是因为以后很多类会继承这两个类,使用时可以不用引用名字空间就直接使用。
* */
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Drawing;
using System.Web;
using System.Web.UI;

namespace CenxaoiWebControls
{
public class WebConfirm : Control,INamingContainer,IPostBackDataHandler
{
/// <summary></summary>
/// 客户端点击“确定”触发的服务端事件.在CliekTrue执行完后,e.OtherMessageData被重置为null。
///
public event EventDataHandler CliekTrue;
/// <summary></summary>
/// 显示的提示信息
///
public string Message
{
get
{
object obj = ViewState["Message"];
return (obj == null) ? String.Empty : (string)obj;
}

set
{
ViewState["Message"] = value;
}
}

/// <summary></summary>
/// 调用ShowConfirmBox函数所带的数据信息,可以在CliekTrue中接收。在CliekTrue执行完后,OtherMessageData被重置为null。
///
protected object OtherMessageData
{
get
{ return ViewState["OtherMessage"]; }
set
{ ViewState["OtherMessage"] = value; }
}

/// <summary></summary>
/// 回发数据的隐藏域ClientID
///
protected string HelperID
{
get
{
return "__" + ClientID + "_State";
}
}
/// <summary></summary>
/// 将控件注册需要回发处理的控件,在初始化时
///
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Page != null)
Page.RegisterRequiresPostBack(this);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);

if (Page != null)
{
Page.RegisterHiddenField(HelperID, "");
}
}

/// <summary></summary>
/// 实现 IPostBackDataHandler 接口
///
bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
{
string value = postCollection[HelperID];
/*
* 因为值从字典中取出,不知是否取到,所有用value关键字存放变量,value可以与null判断。
* 隐藏域有有效值
* */
if (null!=value&&value.ToLower().IndexOf("true")>-1)
return true;
return false; //不触发RaisePostDataChangedEvent事件
}
/// <summary></summary>
/// 实现 IPostBackDataHandler 接口
///
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
if (CliekTrue != null)
{
CliekTrue(this,new EventDataArgs(this.OtherMessageData));
this.OtherMessageData=null; //事件执行后清除this.OtherMessageData引用。
}
}
/// <summary></summary>
/// 客户端发出确认框。
///
/// 在ClickTrue事件中,可以使用到的数据
public void ShowConfirmBox(object otherMessageData)
{
ShowConfirmBox(this.Message,otherMessageData);
}
/// <summary></summary>
/// 客户端发出确认框。
///
/// 在ClickTrue事件中,可以使用到的数据
/// 确认提示信息
public void ShowConfirmBox(string message,object otherMessageData )
{
this.OtherMessageData=otherMessageData;
Page.RegisterStartupScript("__ShowConfirmBox","<script type="text/javascript"> document.getElementById(/""+this.HelperID+"/").value=confirm(/""+Js.jsCode(message)+"/");document.forms[0].submit(); </script> ");
}
}
}


/// <summary></summary>
/// 含有一个Ojbect的事件参数类
///
public class EventDataArgs:System.EventArgs
{
public object EventData;
public EventDataArgs(object eventData)
{
EventData=eventData;
}
}
/// <summary></summary>
/// 对应EventDataArgs的委托
///
public delegate void EventDataHandler(object sender, EventDataArgs e);

public class Js
{
public static string Alert(string s)
{
return "<script type="text/javascript"> alert(/""+jsCode(s)+"/") </script> ";
}
//把字符串转换成客户端js字符串
public static string jsCode(string s)
{
return s.Replace("/t","//t").Replace("/n","//n").Replace("/r","//r").Replace("'","//'").Replace("/"","///"");
}
}
调用例子:
页面文件 WebForm1.aspx

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MakingControls
{
/// <summary></summary>
/// WebForm1 的摘要说明。
///
public class WebForm1 : System.Web.UI.Page
{
protected CenxaoiWebControls.WebConfirm WebConfirm1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary></summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
WebConfirm1.ShowConfirmBox("确认要删除吗?s/nb/tc'/"","del");
}
private void Button2_Click(object sender, System.EventArgs e)
{
WebConfirm1.ShowConfirmBox("确认要审核吗?s/nb/tc'/"","审核");
}
private void WebConfirm1_CliekTrue(object sender, EventDataArgs e)
{
//查看触发提示时的其他信息
if("del"==e.EventData.ToString())
{
//在这里写执行删除的代码。
}
else if("审核"==e.EventData.ToString())
{
//在这里写执行审核的代码。
}
}
}
}

分享到:
评论

相关推荐

    Acro-Multi Language Suit for Delphi win32/64Delphi多语言套件

    https://www.evget.com/product/1409 一组很方便实现多语言的组件。数据字典提供原语与多国文字的映射,多语言套件在程式运行时从文档,数据库,或 DFM 载入数据字典到 Hash 表,以提供最快的搜寻速度;最后根据要...

    devchs20.2.3.ini

    DevExpress.20.2.3 for Delphi 10.4中文语言文件,除函数说明外已经全部翻译 DevExpress.20.2.3 下载地址:...更新日志:https://www.evget.com/article/2020/12/29/39889.html

    LEADTOOLS Multimedia Suite v17.5 Serial Number Generator

    LEADTOOL Multimedia Suite v...http://www.evget.com/zh-CN/product/786/feature.aspx 商业使用请购买支持正版;码农何苦为难码农。 LEADTOOLS官方网站: http://www.leadtools.com/ 再一次,商业使用请支持正版。

    BCGControlBar Pro for MFC(VC扩展界面库)V22.1

    BCGControlBar(Business Components Gallery ControlBar)专业版是MFC的一个扩展库,您可以用来构建类似于Microsoft Office 2000/XP/2003/2007/2010/2013 和 Microsoft Visual ...http://www.evget.com/product/88

    Kendo.UI:来源于别人的网页

    是一个Js框架:https://www.evget.com/demo-sample/8617

    TMS CETools

    13种能在Delphi以及C++Builder应用程序与掌上电脑之间启用完美连接的控件。TCESystemInfo:恢复系统信息 TCERegistry:读写掌上电脑注册表 ...详细特点:http://www.evget.com/zh-CN/product/1672/feature.aspx

    GMU 2.1.1 已合并

    GMU是基于zepto的mobile UI组件库,提供webapp、pad端简单易用的UI组件! 完整包!! 更多phonegap资讯: http://www.evget.com/product/search?k=phonegap

    Delphi Win32多语言套件4.0.5.2145

    http://www.evget.com/zh-CN/product/1409/feature.aspx http://multilanguage.tech.topzj.com 支持delphi5,6,7,2007,2009主要特色: 1、不需要修改1行代码就能多语言化;(或少量修改,如,修改掉没有使用...

    TMS Workflow Studio

    Workflow Studio 是一个基于 Business Process Management (BPM) 的 Delphi VCL framework. 使用 Workflow Studio , 通过允许你或你的终端用户...详细特点介绍:http://www.evget.com/zh-CN/product/1483/feature.aspx

    delphi多语言套件 3.5.2.2023

    http://www.evget.com/zh-CN/product/1409/feature.aspx http://multilanguage.tech.topzj.com/ 一组很方便实现多语言的元件。资料字典提供原语与多国文字的映射,多语言套件在程式运行时从文档,资料库,或 DFM ...

    XtraReports.chm

    dev report帮助文档chm格式文档下载以及dev相关帮助文档下载,附文档下载的百度云链接和提取码 https://www.evget.com/article/2016/8/15/24695.html【两种格式提取码:pxff rere】

    FastReport Studio

    FastReport Studio 是一款针对办公商务提供的软件解决方案。它包括多个强大而彼此独立的功能:报表设计、日程安排编辑、保存和发布。...详细介绍请点击:http://www.evget.com/zh-CN/product/732/feature.aspx

    Web应用性能检测工具Bucky.zip

    BuckyClient 是一款直接从用户浏览器检测Web应用程序性能的免费、开源工具,可以自动获取页面加载情况和AJAX 请求。Bucky不仅快速,高效,而且是轻量级的开源工具,压缩后...介绍来自 evget.com。 标签:Bucky

    Gizmox电子书:从Windows开发走向HTML5开发

    很多人也许不知道Gizmox,但一定知道Visual WebGui ,没错,Visual WebGui就是Gizmox的控件。Visual WebGui是完美的Web开发解决方案,可完全集成到Visual Studio中,并在其中以winform形式...免费下载可以上evget.com

    汉化软件sisulizer个人总结

    个人简单的对sisulizer本地化软件的功能,步骤的简单介绍

Global site tag (gtag.js) - Google Analytics