using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace DSWebComponent { [DefaultProperty("Text")] [ToolboxData("<{0}:ClickOnceButton runat=server>")] public class ClickOnceButton : System.Web.UI.WebControls.Button { /// /// 默认的构造函数。 /// public ClickOnceButton() { this.ViewState["afterSubmitText"] = "正在提交,请稍候..."; base.Text = "ClickOnceButton"; this.ViewState["showMessageBox"] = false; this.ViewState["warningText"] = "确定要提交吗?"; } /// /// 获取或设置单击按钮后,按钮上所显示的文本。 /// [Bindable(true), Category("Appearance"), DefaultValue("正在提交,请稍候..."), Description("指示单击提交后,按钮上所显示的文本。")] public string AfterSubmitText { get { string afterSubmitText = (string)this.ViewState["afterSubmitText"]; if (afterSubmitText != null) { return afterSubmitText; } else { return string.Empty; } } set { this.ViewState["afterSubmitText"] = value; } } [Bindable(true), Category("Appearance"), DefaultValue(false), Description("指示是否要显示一个提示框。")] public bool ShowMessageBox { get { return (bool)this.ViewState["showMessageBox"]; } set { this.ViewState["showMessageBox"] = value; } } [Bindable(true), Category("Appearance"), DefaultValue("确定要提交吗?"), Description("指示提示框内所包含的内容。")] public string WarningText { get { return (string)this.ViewState["warningText"]; } set { this.ViewState["warningText"] = value; } } /// /// AddAttributesToRender /// /// HtmlTextWriter protected override void AddAttributesToRender(HtmlTextWriter writer) { System.Text.StringBuilder ClientSideEventReference = new System.Text.StringBuilder(); if (((this.Page != null) && this.CausesValidation) && (this.Page.Validators.Count > 0)) { ClientSideEventReference.Append("if (typeof(Page_ClientValidate) == 'function'){if (Page_ClientValidate() == false){return false;}}"); } //ShowMessageBox? if (this.ShowMessageBox) { ClientSideEventReference.Append("if (!confirm('" + this.WarningText + "')){return false}"); } ClientSideEventReference.AppendFormat("this.value = '{0}';", (string)this.ViewState["afterSubmitText"]); ClientSideEventReference.Append("this.disabled = true;"); ClientSideEventReference.Append(this.Page.ClientScript.GetPostBackEventReference(this, string.Empty)); writer.AddAttribute(HtmlTextWriterAttribute.Onclick, ClientSideEventReference.ToString(), true); base.AddAttributesToRender(writer); } } }