asp.net2.0自定义控件---弹出确定与取消对话框
2007-12-06 15:47
357 查看
如果想把按钮加一个对话框通常用以下方法:
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
![](http://p.blog.csdn.net/images/p_blog_csdn_net/aspnet2002web/07-9-22ConfBt01.JPG)
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属性里可以输入弹出对话框内容,如果不填写则和普通按钮一样。
![](http://p.blog.csdn.net/images/p_blog_csdn_net/aspnet2002web/07-9-22ConfBt02.JPG)
7、运行效果如下:
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、运行效果如下:
相关文章推荐
- asp.net2.0自定义控件---弹出确定与取消对话框
- C# 里弹出“确定”“取消”对话框
- (转)winform(C#)里弹出“确定”“取消”对话框
- 页面弹出对话框之"确定"与"取消"
- android弹出对话框中带有确定和取消对话框带事件的应用
- QT的一个奇怪问题,设置了Qt::Tool后,点击弹出对话框的确定取消按钮,程序直接退出
- 新手求教android的checkBox问题,现在我想实现按一个按钮,弹出一个对话框,这个对话框有标题,两列多行的checkBox以及最后的确定取消按钮,咋做?
- QT的一个奇怪问题,设置了Qt::Tool后,点击弹出对话框的确定取消按钮,程序直接退出。
- 用javascript如何弹出“确定/取消”对话框?
- QT的一个奇怪问题,设置了Qt::Tool后,点击弹出对话框的确定取消按钮,程序直接退出
- ASP.NET WebForm弹出对话框 确定取消点击事件
- 弹出对话框+隐藏控件实现实现对确定和取消的判断
- 弹出一个对话框提示是“确定”还是“取消”操作
- winform(C#)里弹出“确定”“取消”对话框
- 点击按钮弹出 确定 取消 对话框
- JS中弹出确定和取消对话框并跳转页面
- 在后置代码中弹出确定取消对话框的例子
- JavaScript实现弹出“确定/取消”对话框的方法
- winform(C#)里弹出“确定”“取消”对话框
- 关于在java中弹出确定取消的对话框问题