为了使用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
>
界面可以自己定义,但是必须要有两个Label和两个Button,而且名字必须跟上面的一样.
2. 界面的后台代码:
namespace
Newtop.Common.Web.UserControls
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
using System.Web.UI.WebControls;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public class ConfirmDialog : System.Web.UI.UserControl
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public void InitDialog(ConfirmDialogArgs args)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
![None.gif](/Images/OutliningIndicators/None.gif)
namespace
Newtop.Common.Web.UserControls
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// ConfirmDialgArgs 的摘要说明。
/// </summary>
public class ConfirmDialogArgs
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public ConfirmDialogArgs()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
Title 属性#region Title 属性
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private string title;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
///
/// </summary>
public string Title
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return title;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
title = value;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#endregion
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
Text 属性#region Text 属性
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private string text;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
///
/// </summary>
public string Text
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return text;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
text = value;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#endregion
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
YesCmdHandler 属性#region YesCmdHandler 属性
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private CommandEventHandler yesCmdHandler;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
///
/// </summary>
public CommandEventHandler YesCmdHandler
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return yesCmdHandler;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
yesCmdHandler = value;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#endregion
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
NoCmdHandler 属性#region NoCmdHandler 属性
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private CommandEventHandler noCmdHandler;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
///
/// </summary>
public CommandEventHandler NoCmdHandler
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return noCmdHandler;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
noCmdHandler = value;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#endregion
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
YesCmdArgs 属性#region YesCmdArgs 属性
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private string yesCmdArgs;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
///
/// </summary>
public string YesCmdArgs
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return yesCmdArgs;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
yesCmdArgs = value;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#endregion
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
NoCmdArgs 属性#region NoCmdArgs 属性
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private string noCmdArgs;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
///
/// </summary>
public string NoCmdArgs
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return noCmdArgs;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
noCmdArgs = value;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#endregion
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
}
这个类比较简单,请对照InitDialog方法理解,这里就不在赘述了.
4. 使用方法
其实使用这个东西很简单,分以下几步:
4.1 建立页面.
把 1 中的用户控件放在一个aspx页面中.
4.2 在aspx文件中为对话框初始化一个ConfirmDialogArgs的实例.
private
void
Page_Load(
object
sender, System.EventArgs e)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
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();
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
MyController.State[
"
ConfirmDialogArgs
"
]
=
args;
![None.gif](/Images/OutliningIndicators/None.gif)
MyController.PerformConfirm();
最后那一句是转向显示对话框的页面.
上面代码中有一个DeleteVfSystemCommand的方法,用来作为对话框选择了"是"的时候做委托.下面是他的具体代码:
private
void
DeleteVfSystemCommand(
object
sender,CommandEventArgs e)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
IVfSystemDAO dao = Factory.VfSystemDAO;
dao.DeleteVfSystem(Convert.ToInt32(e.CommandArgument));
MyController.PerformVfSystemManage();
}
现在这个对话框一切正常,都还好用.我这里只是提供一个建议,不喜勿怪.