asp.net2.0自定义控件---弹出确定与取消对话框

来源:互联网 发布:js reduce方法 编辑:程序博客网 时间:2024/06/06 10:59

如果想把按钮加一个对话框通常用以下方法:
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>

            
/// 覆盖此方法以显示版本信息,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);
        }
    }
}

4、LinkButton.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}:LinkButton runat=server></{0}:LinkButton>"
)]
    
public class
 LinkButton : System.Web.UI.WebControls.LinkButton
    {
        
private string _confirmMessage = ""
;

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

        [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);
        }
    }
}

5、生成项目获得ZgdxButton.dll控件文件,加入到ASP.NET工具箱里,共有3个控件
6、当把控件拖放到网页里,查看属性会发现多了一个ConfirmMessage属性,和Help属性。在ConfirmMessage属性里可以输入弹出对话框内容,如果不填写则和普通按钮一样。

7、运行效果如下:


 
原创粉丝点击