/*
* 在看这个控件代码之前,先要熟悉以下内容:
*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())
{
//在这里写执行审核的代码。
}
}
}
}
相关推荐
qmlGUI控件合集qmlGUI控件合集qmlGUI控件合集qmlGUI控件合集qmlGUI控件合集qmlGUI控件合集qmlGUI控件合集qmlGUI控件合集qmlGUI控件合集qmlGUI控件合集qmlGUI控件合集qmlGUI控件合集qmlGUI控件合集qmlGUI控件合集...
PB各种时间控件, PB各种时间控件, PB各种时间控件 PB各种时间控件PB各种时间控件, PB各种时间控件, PB各种时间控件 PB各种时间控件
Web控件和验证控件Web控件和验证控件
ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件ajax控件...
ListCtrl控件和下拉框,编辑框等控件组合使用ListCtrl控件和下拉框,编辑框等控件组合使用ListCtrl控件和下拉框,编辑框等控件组合使用ListCtrl控件和下拉框,编辑框等控件组合使用ListCtrl控件和下拉框,编辑框等...
用c#编写的示例程序,可以单击或双击后拖动控件到窗体任何位置,鼠标移动到控件边沿可以改变光标形状,按下鼠标左键后拖动可以改变控件大小,改变主窗体大小控件大小跟随改变
自定义等待服务器控件,提示和确认控件,进度条控件等.
摘要:VC/C++源码,其它分类,控件ID,控件窗口指针 VC++通过控件ID获得控件窗口指针,点击窗口中的按钮,即可看到控件ID所获取到的控件指针,弹出窗口内显示的内容。VC++通过控件ID获得控件窗口指针源程序代码分享给...
flash控件 flash控件 flash控件flash控件 flash控件 flash控件
c#常用控件的属性方法, 1、窗体 2、Label 控件 3、TextBox 控件 4、RichTextBox控件 5、NumericUpDown 控件 6、Button 控件 7、GroupBox 控件 8、RadioButton控件 9、CheckBox 控件 10、ListBox 控件 11、...
MSChart控件MSChart控件MSChart控件
1.在画布中的控件A长按能进行控件的连接,只有在控件B范围内抬起控件之间的连线才能连接成功; 2.当控件连线成功后,拖动控件AB之间的连线随着控件的拖动随之变化; 3.控件连线在屏幕上随着手势滑动触碰到连接则...
wpf界面上 控件拖动,以及控件之间的位置对换
万年历控件万年历控件万年历控件万年历控件万年历控件万年历控件万年历控件
自定义的c#控件库
第三方验证控件 验证码第三方验证控件 验证码第三方验证控件 验证码第三方验证控件 验证码第三方验证控件 验证码第三方验证控件 验证码第三方验证控件 验证码第三方验证控件 验证码第三方验证控件 验证码第三方验证...
vc++ MFC 界面设计编程 各种控件设计,包括树控件,视图控件,静态文本,按钮控件,旋转控件,组合狂,tab控件等等,欢迎下载啊。(树控件)
本小程序主要展示WinForm用户控件的使用及相互之间的通信,这里主要是通过事件进行实现的,第一个用户控件:(UCForm1.cs)主要包含一个文本框,第二个用户控件:(UCForm2.cs)包含了一个DataGridView和一个文本框,...
用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件用户自定义控件,用户自定义控件,用户自定义控件,用户自定义...
VB控件教程 VB控件大全 VB控件详解 VB控件用法 所有控件介绍