在.net web应用程序开发中, 我们希望用户在做一个重要的操作时, 能够询问或警告用户. 或者希望我们有这么一个简单实用的控件, 能在用户确定后引发一个服务端的事件.
这个控件的原理很简单,主要是实现ipostbackeventhandler接口和调用page.getpostbackeventreference(this, eventargument), 实现对客户端__dopostback方法的调用, 引发服务端的事件
而以下这段关键代码是实现功能的核心:
if(alertstring != "") //仅在用户确认后调用客户端的__dostpostback, 引发服务端事件
{
action = "javascript:if(window.confirm(\" + alertstring + ")==true){";
action += page.getpostbackeventreference(this, "click");
action += ";}";
}
全部代码:
using system;
using system.web.ui;
using system.web.ui.webcontrols;
using system.componentmodel;
namespace booksir.webcontrols
{
/// <summary>
/// alertbutton 的摘要说明。
/// </summary>
[
defaultproperty("text"),
toolboxdata("<{0}:alertbutton runat=server></{0}:alertbutton>"),
system.componentmodel.defaultevent("click"),
]
public class alertbutton : system.web.ui.webcontrols.webcontrol, ipostbackeventhandler
{
private viewstatebag statebag;
public alertbutton()
{
statebag = new viewstatebag(this.viewstate);
}
public event eventhandler click; //事件句柄
public enum appearanceenum
{
button,
imagebutton,
}
/// <summary>
/// 按钮的外观模式
/// </summary>
[
bindable(false),
category("appearance"),
defaultvalue(appearanceenum.button),
description("按钮的外观模式"),
]
public appearanceenum appearance
{
get
{
object obj;
obj = viewstate["appearance"];
if(obj == null)
{
appearance = appearanceenum.button;
return appearanceenum.button;
}
return (appearanceenum)obj;
}
set
{
viewstate["appearance"] = value;
}
}
/// <summary>
/// 在defaultvalue为非常量值的情况下,可以用reset…来重置属性的默认值
/// </summary>
void resetappearance()
{
appearance = appearanceenum.button;
}
/// <summary>
/// 该方法的存在使系统在属性为默认值不提交属性赋值代码
/// </summary>
/// <returns></returns>
bool shouldserializeappearance()
{
return appearance != appearanceenum.button;
}
[
bindable(true),
category("appearance"),
defaultvalue("")
]
public string text
{
get
{
return statebag.getstring("text", this.id);
}
set
{
viewstate["text"] = value;
}
}
/// <summary>
/// 在执行动作前的提示
/// </summary>
[
bindable(true),
category("appearance"),
defaultvalue(""),
description("在执行动作前的提示"),
]
public string alertstring
{
get
{
return statebag.getstring("alertstring", "是否开始执行?");
}
set
{
viewstate["alertstring"] = value;
}
}
/// <summary>
/// 按钮可用时的image
/// </summary>
[
description("按钮可用时的image"),
category("appearance"),
editor(typeof(system.web.ui.design.urleditor), typeof(system.drawing.design.uitypeeditor)),
]
public string enabledimage
{
get
{
return statebag.getstring("enabledimage", "");
}
set
{
viewstate["enabledimage"] = value;
}
}
/// <summary>
/// 按钮不可用时的image
/// </summary>
[
description("按钮不可用时的image"),
category("appearance"),
editor(typeof(system.web.ui.design.urleditor), typeof(system.drawing.design.uitypeeditor)),
]
public string disabledimage
{
get
{
return statebag.getstring("disabledimage", "");
}
set
{
viewstate["disabledimage"] = value;
}
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 html 编写器 </param>
protected override void render(htmltextwriter output)
{
if(appearance == appearanceenum.button)
output.write(getbuttonhtml());
else
output.write(getimagebuttonhtml());
}
/// <summary>
/// 获取呈现button时的html
/// </summary>
/// <returns></returns>
private string getbuttonhtml()
{
const string buttontag = "<input type=button value={0} onclick=\"{1}\" style=\"{2}\"{3} title={4}>";
string shtml;
string action;
string style = "width:{0};height:{1};";
if(alertstring != "")
{
action = "javascript:if(window.confirm(\" + alertstring + ")==true){";
action += page.getpostbackeventreference(this, "click");
action += ";}";
}
else
action = "javascript:" + page.getpostbackeventreference(this, "click");
style = string.format
(
style,
this.width.tostring(),
this.height.tostring()
);
style += this.attributes["style"];
shtml = string.format
(
buttontag,
text,
action,
style,
enabled ? "" : " disabled",
this.tooltip
);
return shtml;
}
/// <summary>
/// 获取呈现imagebutton时的html
/// </summary>
/// <returns></returns>
private string getimagebuttonhtml()
{
const string linktag = "<a onclick=\"{0}\" title={1} style=\"{2}\">{3}</a>";
const string imgtag = "<img src={0} border=0>";
string shtml;
string action;
string image;
string style;
if(this.enabled)
{
if(alertstring != "") //仅在用户确认后调用客户端的__dostpostback, 引发服务端事件
{
action = "javascript:if(window.confirm(\" + alertstring + ")==true){";
action += page.getpostbackeventreference(this, "click");
action += ";}";
}
else
action = "javascript:" + page.getpostbackeventreference(this, "click");
if(enabledimage != "")
image = string.format(imgtag, enabledimage);
else
image = text;
}
else
{
action = "javascript:void()";
if(disabledimage != "")
image = string.format(imgtag, disabledimage);
else
image = text;
}
style = "cursor:hand;";
style += this.attributes["style"];
shtml = string.format
(
linktag,
action,
this.tooltip,
style,
image
);
return shtml;
}
protected virtual void onclick()
{
if(click != null)
click(this, eventargs.empty);
}
public void raisepostbackevent(string eventargument)
{
if(eventargument == "click")
onclick();
}
}
}
好了, 到此结束, 将以上代码编译为dll, 并加入为控件吧, 试试看, 是不是简单实用呢?