为了使用CNBLOG上的服务器端ConfirmDialog,我自己做了一个,用起来还不错.
1. 界面:
<%&#64; Control Language&#61;"c#" AutoEventWireup&#61;"false" Codebehind&#61;"ConfirmDialog.ascx.cs" Inherits&#61;"Newtop.Common.Web.UserControls.ConfirmDialog" TargetSchema&#61;"http://schemas.microsoft.com/intellisense/ie5" %>
<div id&#61;"Header" class&#61;"Dialog">
<div id&#61;"Header_Header" class&#61;"DialogTitle">
<span>
<asp:Label id&#61;"lbTitle" runat&#61;"server">Labelasp:Label>
span>
div>
<div id&#61;"Header_Contents" class&#61;"DialogBody">
<span id&#61;"Header_lblOutput">
<asp:Label Runat&#61;"server" id&#61;"lbText">Dialog Textasp:Label>span>
<div style&#61;"MARGIN-TOP: 12px">
<asp:LinkButton Runat&#61;"server" CssClass&#61;"Button" id&#61;"btnYes" CommandName&#61;"Yes">Yesasp:LinkButton>
<asp:LinkButton Runat&#61;"server" CssClass&#61;"Button" id&#61;"btnNo" CommandName&#61;"No">Noasp:LinkButton>
<BR>
div>
div>
div>
<div id&#61;"Header" class&#61;"Dialog">
<div id&#61;"Header_Header" class&#61;"DialogTitle">
<span>
<asp:Label id&#61;"lbTitle" runat&#61;"server">Labelasp:Label>
span>
div>
<div id&#61;"Header_Contents" class&#61;"DialogBody">
<span id&#61;"Header_lblOutput">
<asp:Label Runat&#61;"server" id&#61;"lbText">Dialog Textasp:Label>span>
<div style&#61;"MARGIN-TOP: 12px">
<asp:LinkButton Runat&#61;"server" CssClass&#61;"Button" id&#61;"btnYes" CommandName&#61;"Yes">Yesasp:LinkButton>
<asp:LinkButton Runat&#61;"server" CssClass&#61;"Button" id&#61;"btnNo" CommandName&#61;"No">Noasp:LinkButton>
<BR>
div>
div>
div>
界面可以自己定义,但是必须要有两个Label和两个Button,而且名字必须跟上面的一样.
2. 界面的后台代码:
namespace Newtop.Common.Web.UserControls
{
using System.Web.UI.WebControls;
public class ConfirmDialog : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label lbTitle;
protected System.Web.UI.WebControls.LinkButton btnYes;
protected System.Web.UI.WebControls.LinkButton btnNo;
protected System.Web.UI.WebControls.Label lbText;
public void InitDialog(ConfirmDialogArgs args)
{
lbTitle.Text &#61; args.Title;
lbText.Text &#61; args.Text;
btnYes.Command &#43;&#61; args.YesCmdHandler;
btnNo.Command &#43;&#61; args.NoCmdHandler;
btnYes.CommandArgument &#61; args.YesCmdArgs;
btnNo.CommandArgument &#61; args.NoCmdArgs;
}
}
}
{
using System.Web.UI.WebControls;
public class ConfirmDialog : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label lbTitle;
protected System.Web.UI.WebControls.LinkButton btnYes;
protected System.Web.UI.WebControls.LinkButton btnNo;
protected System.Web.UI.WebControls.Label lbText;
public void InitDialog(ConfirmDialogArgs args)
{
lbTitle.Text &#61; args.Title;
lbText.Text &#61; args.Text;
btnYes.Command &#43;&#61; args.YesCmdHandler;
btnNo.Command &#43;&#61; args.NoCmdHandler;
btnYes.CommandArgument &#61; args.YesCmdArgs;
btnNo.CommandArgument &#61; args.NoCmdArgs;
}
}
}
类中关键是InitDialog方法,他接收一个ConfirmDialogArgs的实例作为参数,用来初始化Dialog.
3. ConfirmDialogArgs类
using System;
using System.Web.UI.WebControls;
namespace Newtop.Common.Web.UserControls
{
/**////
/// ConfirmDialgArgs 的摘要说明。
///
public class ConfirmDialogArgs
{
public ConfirmDialogArgs()
{
}
Title 属性#region Title 属性
private string title;
/**////
///
///
public string Title
{
get
{
return title;
}
set
{
title &#61; value;
}
}
#endregion
Text 属性#region Text 属性
private string text;
/**////
///
///
public string Text
{
get
{
return text;
}
set
{
text &#61; value;
}
}
#endregion
YesCmdHandler 属性#region YesCmdHandler 属性
private CommandEventHandler yesCmdHandler;
/**////
///
///
public CommandEventHandler YesCmdHandler
{
get
{
return yesCmdHandler;
}
set
{
yesCmdHandler &#61; value;
}
}
#endregion
NoCmdHandler 属性#region NoCmdHandler 属性
private CommandEventHandler noCmdHandler;
/**////
///
///
public CommandEventHandler NoCmdHandler
{
get
{
return noCmdHandler;
}
set
{
noCmdHandler &#61; value;
}
}
#endregion
YesCmdArgs 属性#region YesCmdArgs 属性
private string yesCmdArgs;
/**////
///
///
public string YesCmdArgs
{
get
{
return yesCmdArgs;
}
set
{
yesCmdArgs &#61; value;
}
}
#endregion
NoCmdArgs 属性#region NoCmdArgs 属性
private string noCmdArgs;
/**////
///
///
public string NoCmdArgs
{
get
{
return noCmdArgs;
}
set
{
noCmdArgs &#61; value;
}
}
#endregion
}
}
using System.Web.UI.WebControls;
namespace Newtop.Common.Web.UserControls
{
/**////
/// ConfirmDialgArgs 的摘要说明。
///
public class ConfirmDialogArgs
{
public ConfirmDialogArgs()
{
}
Title 属性#region Title 属性
private string title;
/**////
///
///
public string Title
{
get
{
return title;
}
set
{
title &#61; value;
}
}
#endregion
Text 属性#region Text 属性
private string text;
/**////
///
///
public string Text
{
get
{
return text;
}
set
{
text &#61; value;
}
}
#endregion
YesCmdHandler 属性#region YesCmdHandler 属性
private CommandEventHandler yesCmdHandler;
/**////
///
///
public CommandEventHandler YesCmdHandler
{
get
{
return yesCmdHandler;
}
set
{
yesCmdHandler &#61; value;
}
}
#endregion
NoCmdHandler 属性#region NoCmdHandler 属性
private CommandEventHandler noCmdHandler;
/**////
///
///
public CommandEventHandler NoCmdHandler
{
get
{
return noCmdHandler;
}
set
{
noCmdHandler &#61; value;
}
}
#endregion
YesCmdArgs 属性#region YesCmdArgs 属性
private string yesCmdArgs;
/**////
///
///
public string YesCmdArgs
{
get
{
return yesCmdArgs;
}
set
{
yesCmdArgs &#61; value;
}
}
#endregion
NoCmdArgs 属性#region NoCmdArgs 属性
private string noCmdArgs;
/**////
///
///
public string NoCmdArgs
{
get
{
return noCmdArgs;
}
set
{
noCmdArgs &#61; value;
}
}
#endregion
}
}
这个类比较简单,请对照InitDialog方法理解,这里就不在赘述了.
4. 使用方法
其实使用这个东西很简单,分以下几步:
4.1 建立页面.
把 1 中的用户控件放在一个aspx页面中.
4.2 在aspx文件中为对话框初始化一个ConfirmDialogArgs的实例.
private void Page_Load(object sender, System.EventArgs e)
{
ConfirmDialog dialog &#61; FindControl("confirmDialog") as ConfirmDialog;
ConfirmDialogArgs args &#61; MyController.State["ConfirmDialogArgs"] as ConfirmDialogArgs;
dialog.InitDialog(args);
}
{
ConfirmDialog dialog &#61; FindControl("confirmDialog") as ConfirmDialog;
ConfirmDialogArgs args &#61; MyController.State["ConfirmDialogArgs"] as ConfirmDialogArgs;
dialog.InitDialog(args);
}
因为我的程序中用了UIP,所以,我这里是直接从State中取出了一个ConfirmDialogArgs的实例.
下面我们来看一个初始化ConfirmDialogArgs的例子:
ConfirmDialogArgs args &#61; new ConfirmDialogArgs();
args.Title &#61; "Delete";
args.Text &#61; "Delete it!";
args.YesCmdHandler &#61; new CommandEventHandler(this.DeleteVfSystemCommand);
args.YesCmdArgs &#61; e.CommandArgument.ToString();
MyController.State["ConfirmDialogArgs"] &#61; args;
MyController.PerformConfirm();
args.Title &#61; "Delete";
args.Text &#61; "Delete it!";
args.YesCmdHandler &#61; new CommandEventHandler(this.DeleteVfSystemCommand);
args.YesCmdArgs &#61; e.CommandArgument.ToString();
MyController.State["ConfirmDialogArgs"] &#61; args;
MyController.PerformConfirm();
最后那一句是转向显示对话框的页面.
上面代码中有一个DeleteVfSystemCommand的方法,用来作为对话框选择了"是"的时候做委托.下面是他的具体代码:
private void DeleteVfSystemCommand(object sender,CommandEventArgs e)
{
IVfSystemDAO dao &#61; Factory.VfSystemDAO;
dao.DeleteVfSystem(Convert.ToInt32(e.CommandArgument));
MyController.PerformVfSystemManage();
}
{
IVfSystemDAO dao &#61; Factory.VfSystemDAO;
dao.DeleteVfSystem(Convert.ToInt32(e.CommandArgument));
MyController.PerformVfSystemManage();
}
现在这个对话框一切正常,都还好用.我这里只是提供一个建议,不喜勿怪.