asp.net2.0自訂控制項---彈出確定與取消對話方塊

來源:互聯網
上載者:User



如果想把按鈕加一個對話方塊通常用以下方法:
Button1.Attributes.Add("OnClick", "return confirm('是否提交。');");
Button1.Attributes.Add("onclick", "if(confirm('是否提交。')){return true;}else{return false;}");
現在我們給她做成控制項吧。
1、建立項目ZgdxButton和三個類NormalButton.cs,ImgButton.cs, LinkButton.cs

2、NormalButton.cs代碼如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace ZgdxButton
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:NormalButton runat=server></{0}:NormalButton>")]
    public class NormalButton : System.Web.UI.WebControls.Button
    {
        private string _confirmMessage = "";

        public NormalButton()
            : base()
        {
            base.Text = "Button";
        }

        [Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("輸入彈出提示內容,延邊職大(中國.延吉)")]
        public string ConfirmMessage
        {
            get
            {
                return _confirmMessage;
            }
            set
            {
                _confirmMessage = value;
            }
        }

        //EditorAttribute:指定用來更改屬性的編輯器version
        /// <summary>
        /// 指定控制項的協助資訊
        /// </summary>
        private string _help = "協助";
        [CategoryAttribute("自訂編輯器"),
        DefaultValueAttribute("1.0"),
        DescriptionAttribute("協助資訊,延邊職大(中國.延吉)"),
        ReadOnlyAttribute(true),
        EditorAttribute(typeof(AppVerConverter), typeof(System.Drawing.Design.UITypeEditor))]
        public string Help
        {
            get { return this._help; }
            set { this._help = value; }
        }

        /// <summary>
        /// 自訂UI的屬性編輯器(彈出訊息)
        /// </summary>
        public class AppVerConverter : System.Drawing.Design.UITypeEditor
        {
            /// <summary>
            /// 覆蓋此方法以返回編輯器的類型。
            /// </summary>
            public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
            {
                return System.Drawing.Design.UITypeEditorEditStyle.Modal;
            }

            /// <summary>
            /// 覆蓋此方法以顯示版本資訊,Button控制項,版本V1.0
            /// </summary>
            public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
            {
                string help = " 彈出提示按鈕控制項協助資訊 ";
                help += "主要屬性: ";
                help += "ConfirmMessage屬性:填寫彈出的提示資訊,不寫則和普通控制項一樣。 ";


                help += " 開發作者:延邊職大.許老師線上支援:xuhongkaicn@yahoo.com.cn";
                help += " 版本更新: Version1.0:2007年";

                System.Windows.Forms.MessageBox.Show(help, "協助資訊");
                return value;
            }
        }
        /// <summary>
        /// 將此控制項呈現給指定的輸出參數。
        /// </summary>
        /// <param name="output"> 要寫出到的 HTML 編寫器 </param>
        protected override void Render(HtmlTextWriter output)
        {
            if (this._confirmMessage != "")
            {
                base.Attributes.Add("OnClick", "return confirm('" + this._confirmMessage + "');");
            }
            base.Render(output);
        }
    }
}

3、ImgButton.cs代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace ZgdxButton
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ImgButton runat=server></{0}:ImgButton>")]
    public class ImgButton : System.Web.UI.WebControls.ImageButton
    {
        private string _confirmMessage = "";



        [Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("輸入彈出提示內容,延邊職大(中國.延吉)")]
        public string ConfirmMessage
        {
            get
            {
                return _confirmMessage;
            }
            set
            {
                _confirmMessage = value;
            }
        }

        //EditorAttribute:指定用來更改屬性的編輯器version
        /// <summary>
        /// 指定控制項的協助資訊
        /// </summary>
        private string _help = "協助";
        [CategoryAttribute("自訂編輯器"),
        DefaultValueAttribute("1.0"),
        DescriptionAttribute("協助資訊,延邊職大(中國.延吉)"),
        ReadOnlyAttribute(true),
        EditorAttribute(typeof(AppVerConverter), typeof(System.Drawing.Design.UITypeEditor))]
        public string Help
        {
            get { return this._help; }
            set { this._help = value; }
        }

        /// <summary>
        /// 自訂UI的屬性編輯器(彈出訊息)
        /// </summary>
        public class AppVerConverter : System.Drawing.Design.UITypeEditor
        {
            /// <summary>
            /// 覆蓋此方法以返回編輯器的類型。
            /// </summary>
            public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
            {
                return System.Drawing.Design.UITypeEditorEditStyle.Modal;
            }

            /// <summary>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.