• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

AlertButton, 您确定要执行吗?

发布: 2007-7-01 11:09 | 作者: admin | 来源: | 查看: 14次 | 进入软件测试论坛讨论

领测软件测试网
    在.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, 并加入为控件吧, 试试看, 是不是简单实用呢?

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网