国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

AlertButton, 您確定要執行嗎?

2024-07-21 02:16:09
字體:
來源:轉載
供稿:網友

    在.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, 并加入為控件吧, 試試看, 是不是簡單實用呢?
  • 網站運營seo文章大全
  • 提供全面的站長運營經驗及seo技術!
  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 乌恰县| 开封县| 齐河县| 收藏| 西和县| 庆元县| 龙南县| 都江堰市| 营口市| 新建县| 金沙县| 香河县| 泰兴市| 湖南省| 井冈山市| 会理县| 长沙县| 宜宾市| 鄂温| 娄烦县| 琼结县| 武乡县| 柯坪县| 万山特区| 寻甸| 徐水县| 常熟市| 奉化市| 嘉义市| 井陉县| 淮滨县| 旬邑县| 蒙阴县| 龙井市| 新龙县| 赫章县| 乌兰察布市| 临高县| 海盐县| 桃源县| 毕节市|