 
                    
                
         
    
    
    
	
	
	
	
		
    
 
 在.Net  Web应用程序开发中, 我们希望用户在做一个重要的操作时, 能够询问或警告用户.  或者希望我们有这么一个简单实用的控件, 能在用户确定后引发一个服务端的事件.
      在.Net  Web应用程序开发中, 我们希望用户在做一个重要的操作时, 能够询问或警告用户.  或者希望我们有这么一个简单实用的控件, 能在用户确定后引发一个服务端的事件.
 
 这个控件的原理很简单,主要是实现IPostBackEventHandler接口和调用Page.GetPostBackEventReference(this, eventArgument),  实现对客户端__doPostBack方法的调用, 引发服务端的事件
这个控件的原理很简单,主要是实现IPostBackEventHandler接口和调用Page.GetPostBackEventReference(this, eventArgument),  实现对客户端__doPostBack方法的调用, 引发服务端的事件
 
 而以下这段关键代码是实现功能的核心:
而以下这段关键代码是实现功能的核心:
 
 if(AlertString != "") //仅在用户确认后调用客户端的__DostPostBack, 引发服务端事件
    if(AlertString != "") //仅在用户确认后调用客户端的__DostPostBack, 引发服务端事件 
 {
    { 
 Action = if(window.confirm(\'"  + AlertString + "')==true){";
     Action = if(window.confirm(\'"  + AlertString + "')==true){"; 
 Action += Page.GetPostBackEventReference(this, "Click");
     Action += Page.GetPostBackEventReference(this, "Click"); 
 Action += ";}";
     Action += ";}"; 
 }
    } 
 
 全部代码:
全部代码:
 
 using System;
using System; 
 using System.Web.UI;
using System.Web.UI; 
 using System.Web.UI.WebControls;
using System.Web.UI.WebControls; 
 using System.ComponentModel;
using System.ComponentModel; 
 
 namespace Booksir.WebControls
namespace Booksir.WebControls 
 {
{ 
 /// <summary>
 /// <summary> 
 /// AlertButton 的摘要说明。
 /// AlertButton 的摘要说明。 
 /// </summary>
 /// </summary> 
 [
 [ 
 DefaultProperty("Text"),
 DefaultProperty("Text"), 
 ToolboxData("<{0}:AlertButton runat=server></{0}:AlertButton>"),
 ToolboxData("<{0}:AlertButton runat=server></{0}:AlertButton>"), 
 System.ComponentModel.DefaultEvent("Click"),
 System.ComponentModel.DefaultEvent("Click"), 
 ]
 ] 
 public class AlertButton : System.Web.UI.WebControls.WebControl, IPostBackEventHandler
 public class AlertButton : System.Web.UI.WebControls.WebControl, IPostBackEventHandler 
 {
 { 
 private ViewStateBag StateBag;
  private ViewStateBag StateBag; 
 
 public AlertButton()
  public AlertButton() 
 {
  { 
 StateBag = new ViewStateBag(this.ViewState);
   StateBag = new ViewStateBag(this.ViewState); 
 }
  } 
 
 public event EventHandler Click; //事件句柄
  public event EventHandler Click; //事件句柄
 
 public enum AppearanceEnum
  public enum AppearanceEnum 
 {
  { 
 Button,
   Button, 
 ImageButton,
   ImageButton, 
 }
  } 
 
 /// <summary>
  /// <summary> 
 /// 按钮的外观模式
  /// 按钮的外观模式 
 /// </summary>
  /// </summary> 
 [
  [ 
 Bindable(false),
  Bindable(false), 
 Category("Appearance"),
  Category("Appearance"), 
 DefaultValue(AppearanceEnum.Button),
  DefaultValue(AppearanceEnum.Button), 
 Description("按钮的外观模式"),
  Description("按钮的外观模式"), 
 ]
  ] 
 public AppearanceEnum Appearance
  public AppearanceEnum Appearance 
 {
  { 
 get
   get 
 {
   { 
 object obj;
    object obj; 
 obj = ViewState["Appearance"];
    obj = ViewState["Appearance"]; 
 if(obj == null)
    if(obj == null) 
 {
    { 
 Appearance = AppearanceEnum.Button;
     Appearance = AppearanceEnum.Button; 
 return AppearanceEnum.Button;
     return AppearanceEnum.Button; 
 }
    } 
 return (AppearanceEnum)obj;
    return (AppearanceEnum)obj; 
 }
   } 
 set
   set 
 {
   { 
 ViewState["Appearance"] = value;
    ViewState["Appearance"] = value; 
 }
   } 
 }
  } 
 
 /// <summary>
  /// <summary> 
 /// 在DefaultValue为非常量值的情况下,可以用Reset
  /// 在DefaultValue为非常量值的情况下,可以用Reset 来重置属性的默认值
来重置属性的默认值 
 /// </summary>
  /// </summary> 
 void ResetAppearance()
  void ResetAppearance() 
 {
  { 
 Appearance = AppearanceEnum.Button;
   Appearance = AppearanceEnum.Button; 
 }
  } 
 
 /// <summary>
  /// <summary> 
 /// 该方法的存在使系统在属性为默认值不提交属性赋值代码
  /// 该方法的存在使系统在属性为默认值不提交属性赋值代码 
 /// </summary>
  /// </summary> 
 /// <returns></returns>
  /// <returns></returns> 
 bool ShouldSerializeAppearance()
  bool ShouldSerializeAppearance() 
 {
  { 
 return Appearance != AppearanceEnum.Button;
   return Appearance != AppearanceEnum.Button; 
 }
  } 
 
 [
  [ 
 Bindable(true),
  Bindable(true), 
 Category("Appearance"),
  Category("Appearance"), 
 DefaultValue("")
  DefaultValue("") 
 ]
  ] 
 public string Text
  public string Text 
 {
  { 
 get
   get 
 {
   { 
 return StateBag.GetString("Text", this.ID);
    return StateBag.GetString("Text", this.ID); 
 }
   } 
 
 set
   set 
 {
   { 
 ViewState["Text"] = value;
    ViewState["Text"] = value; 
 }
   } 
 }
  } 
 
 /// <summary>
  /// <summary> 
 /// 在执行动作前的提示
  /// 在执行动作前的提示 
 /// </summary>
  /// </summary> 
 [
  [ 
 Bindable(true),
  Bindable(true), 
 Category("Appearance"),
  Category("Appearance"), 
 DefaultValue(""),
  DefaultValue(""), 
 Description("在执行动作前的提示"),
  Description("在执行动作前的提示"), 
 ]
  ] 
 public string AlertString
  public string AlertString 
 {
  { 
 get
   get 
 {
   { 
 return StateBag.GetString("AlertString", "是否开始执行?");
    return StateBag.GetString("AlertString", "是否开始执行?"); 
 }
   } 
 set
   set 
 {
   { 
 ViewState["AlertString"] = value;
    ViewState["AlertString"] = value; 
 }
   } 
 }
  } 
 
 /// <summary>
  /// <summary> 
 /// 按钮可用时的Image
  /// 按钮可用时的Image 
 /// </summary>
  /// </summary> 
 [
  [ 
 Description("按钮可用时的Image"),
  Description("按钮可用时的Image"), 
 Category("Appearance"),
  Category("Appearance"), 
 Editor(typeof(System.Web.UI.Design.UrlEditor), typeof(System.Drawing.Design.UITypeEditor)),
  Editor(typeof(System.Web.UI.Design.UrlEditor), typeof(System.Drawing.Design.UITypeEditor)), 
 ]
  ] 
 public string EnabledImage
  public string EnabledImage 
 {
  { 
 get
   get 
 {
   { 
 return StateBag.GetString("EnabledImage", "");
    return StateBag.GetString("EnabledImage", ""); 
 }
   } 
 set
   set 
 {
   { 
 ViewState["EnabledImage"] = value;
    ViewState["EnabledImage"] = value; 
 }
   } 
 }
  } 
 
 /// <summary>
  /// <summary> 
 /// 按钮不可用时的Image
  /// 按钮不可用时的Image 
 /// </summary>
  /// </summary> 
 [
  [ 
 Description("按钮不可用时的Image"),
  Description("按钮不可用时的Image"), 
 Category("Appearance"),
  Category("Appearance"), 
 Editor(typeof(System.Web.UI.Design.UrlEditor), typeof(System.Drawing.Design.UITypeEditor)),
  Editor(typeof(System.Web.UI.Design.UrlEditor), typeof(System.Drawing.Design.UITypeEditor)), 
 ]
  ] 
 public string DisabledImage
  public string DisabledImage 
 {
  { 
 get
   get 
 {
   { 
 return StateBag.GetString("DisabledImage", "");
    return StateBag.GetString("DisabledImage", ""); 
 }
   } 
 set
   set 
 {
   { 
 ViewState["DisabledImage"] = value;
    ViewState["DisabledImage"] = value; 
 }
   } 
 }
  } 
 
 /// <summary>
  /// <summary> 
 /// 将此控件呈现给指定的输出参数。
  /// 将此控件呈现给指定的输出参数。 
 /// </summary>
  /// </summary> 
 /// <param name="output"> 要写出到的 HTML 编写器 </param>
  /// <param name="output"> 要写出到的 HTML 编写器 </param> 
 protected override void Render(HtmlTextWriter output)
  protected override void Render(HtmlTextWriter output) 
 {
  { 
 if(Appearance == AppearanceEnum.Button)
   if(Appearance == AppearanceEnum.Button) 
 output.Write(GetButtonHtml());
    output.Write(GetButtonHtml()); 
 else
   else 
 output.Write(GetImageButtonHtml());
    output.Write(GetImageButtonHtml()); 
 }
  } 
 
 /// <summary>
  /// <summary> 
 /// 获取呈现Button时的Html
  /// 获取呈现Button时的Html 
 /// </summary>
  /// </summary> 
 /// <returns></returns>
  /// <returns></returns> 
 private string GetButtonHtml()
  private string GetButtonHtml() 
 {
  { 
 const string ButtonTag = "<input type=button value='{0}' onclick=\"{1}\" style=\"{2}\"{3} title='{4}'>";
   const string ButtonTag = "<input type=button value='{0}' onclick=\"{1}\" style=\"{2}\"{3} title='{4}'>"; 
 string sHtml;
   string sHtml; 
 string Action;
   string Action; 
 string Style = "width:{0};height:{1};";
   string Style = "width:{0};height:{1};"; 
 if(AlertString != "")
   if(AlertString != "") 
 {
   { 
 Action = if(window.confirm(\'"  + AlertString + "')==true){";
    Action = if(window.confirm(\'"  + AlertString + "')==true){"; 
 Action += Page.GetPostBackEventReference(this, "Click");
    Action += Page.GetPostBackEventReference(this, "Click"); 
 Action += ";}";
    Action += ";}"; 
 }
   } 
 else
   else 
 Action = " + Page.GetPostBackEventReference(this, "Click");
    Action = " + Page.GetPostBackEventReference(this, "Click"); 
 
 Style = String.Format
   Style = String.Format 
 (
    ( 
 Style,
    Style, 
 this.Width.ToString(),
    this.Width.ToString(), 
 this.Height.ToString()
    this.Height.ToString() 
 );
    ); 
 Style += this.Attributes["Style"];
   Style += this.Attributes["Style"]; 
 sHtml = String.Format
   sHtml = String.Format 
 (
    ( 
 ButtonTag,
    ButtonTag, 
 Text,
    Text, 
 Action,
    Action, 
 Style,
    Style, 
 Enabled ? "" : " disabled",
    Enabled ? "" : " disabled", 
 this.ToolTip
    this.ToolTip 
 );
    ); 
 return sHtml;
   return sHtml; 
 }
  } 
 
 /// <summary>
  /// <summary> 
 /// 获取呈现ImageButton时的Html
  /// 获取呈现ImageButton时的Html 
 /// </summary>
  /// </summary> 
 /// <returns></returns>
  /// <returns></returns> 
 private string GetImageButtonHtml()
  private string GetImageButtonHtml() 
 {
  { 
 const string LinkTag = "<a onclick=\"{0}\" title='{1}' style=\"{2}\">{3}</a>";
   const string LinkTag = "<a onclick=\"{0}\" title='{1}' style=\"{2}\">{3}</a>"; 
 const string ImgTag = "<img src='{0}' border=0>";
   const string ImgTag = "<img src='{0}' border=0>"; 
 string sHtml;
   string sHtml; 
 string Action;
   string Action; 
 string Image;
   string Image; 
 string Style;
   string Style; 
 
 if(this.Enabled)
   if(this.Enabled) 
 {
   { 
 if(AlertString != "") //仅在用户确认后调用客户端的__DostPostBack, 引发服务端事件
    if(AlertString != "") //仅在用户确认后调用客户端的__DostPostBack, 引发服务端事件 
 {
    { 
 Action = if(window.confirm(\'"  + AlertString + "')==true){";
     Action = if(window.confirm(\'"  + AlertString + "')==true){"; 
 Action += Page.GetPostBackEventReference(this, "Click");
     Action += Page.GetPostBackEventReference(this, "Click"); 
 Action += ";}";
     Action += ";}"; 
 }
    } 
 else
    else 
 Action = " + Page.GetPostBackEventReference(this, "Click");
     Action = " + Page.GetPostBackEventReference(this, "Click"); 
 if(EnabledImage != "")
    if(EnabledImage != "") 
 Image = String.Format(ImgTag, EnabledImage);
     Image = String.Format(ImgTag, EnabledImage); 
 else
    else 
 Image = Text;
     Image = Text; 
 }
   } 
 else
   else 
 {
   { 
 Action = void()";
    Action = void()"; 
 if(DisabledImage != "")
    if(DisabledImage != "") 
 Image = String.Format(ImgTag, DisabledImage);
     Image = String.Format(ImgTag, DisabledImage); 
 else
    else 
 Image = Text;
     Image = Text; 
 }
   } 
 Style = "cursor:hand;";
   Style = "cursor:hand;"; 
 Style += this.Attributes["Style"];
   Style += this.Attributes["Style"]; 
 sHtml = String.Format
   sHtml = String.Format 
 (
    ( 
 LinkTag,
    LinkTag, 
 Action,
    Action, 
 this.ToolTip,
    this.ToolTip, 
 Style,
    Style, 
 Image
    Image 
 );
    ); 
 return sHtml;
   return sHtml; 
 }
  } 
 
 protected virtual void OnClick()
  protected virtual void OnClick() 
 {
  { 
 if(Click != null)
   if(Click != null) 
 Click(this, EventArgs.Empty);
    Click(this, EventArgs.Empty); 
 }
  } 
 
 public void RaisePostBackEvent(string eventArgument)
  public void RaisePostBackEvent(string eventArgument) 
 {
  { 
 if(eventArgument == "Click")
   if(eventArgument == "Click") 
 OnClick();
    OnClick(); 
 }
  } 
 }
 } 
 }
} 
 
 
 
 
 
 
