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