`
happmaoo
  • 浏览: 4335998 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

WebConfirm控件

阅读更多
function StorePage() { d=document; t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():''); void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes')); keyit.focus(); }

/*
* 在看这个控件代码之前,先要熟悉以下内容:
*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>
/// 客户端点击“确定”触发的服务端事件.在CliekTrue执行完后,e.OtherMessageData被重置为null。
/// </summary>
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>
/// 调用ShowConfirmBox函数所带的数据信息,可以在CliekTrue中接收。在CliekTrue执行完后,OtherMessageData被重置为null。
/// </summary>
protected object OtherMessageData
{
get
{return ViewState["OtherMessage"];}
set
{ViewState["OtherMessage"] = value;}
}

/// <summary>
/// 回发数据的隐藏域ClientID
/// </summary>
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>
/// 实现 IPostBackDataHandler 接口
/// </summary>
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>
/// 实现 IPostBackDataHandler 接口
/// </summary>
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
if (CliekTrue != null)
{
CliekTrue(this,new EventDataArgs(this.OtherMessageData));
this.OtherMessageData=null;//事件执行后清除this.OtherMessageData引用。
}
}
/// <summary>
/// 客户端发出确认框。
/// </summary>
/// <param name="OtherMessageData">在ClickTrue事件中,可以使用到的数据</param>
public void ShowConfirmBox(object otherMessageData)
{
ShowConfirmBox(this.Message,otherMessageData);
}
/// <summary>
/// 客户端发出确认框。
/// </summary>
/// <param name="OtherMessageData">在ClickTrue事件中,可以使用到的数据</param>
/// <param name="message">确认提示信息</param>
public void ShowConfirmBox(string message,object otherMessageData )
{
this.OtherMessageData=otherMessageData;
Page.RegisterStartupScript("__ShowConfirmBox","<Script>document.getElementById(\""+this.HelperID+"\").value=confirm(\""+Js.jsCode(message)+"\");document.forms[0].submit();</Script>");
}
}
}


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

public class Js
{
public static string Alert(string s)
{
return "<script>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

<%@ Register TagPrefix="cc1" Namespace="CenxaoiWebControls" Assembly="MakingControls" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="MakingControls.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script>

</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="删除"></asp:Button>
<asp:Button id="Button2" runat="server" Text="审核"></asp:Button>
<cc1:WebConfirm id="WebConfirm1" runat="server"></cc1:WebConfirm>
</form>
</body>
</HTML>


页面代码文件 WebForm1.aspx.cs

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>
/// WebForm1 的摘要说明。
/// </summary>
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())
{
//在这里写执行审核的代码。
}
}
}
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics