欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【ASP.NET自定义Web服务器控件之Button控件】,下面是详细的讲解!
ASP.NET自定义Web服务器控件之Button控件
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//自定义web服务器button
namespace MyControls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyButton runat=server></{0}:MyButton>")]
public class MyButton : WebControl,IPostBackEventHandler
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s=(String)ViewState["Text"];
return ((s==null) ? String.Empty : s);
}
set
{
ViewState["Text"]=value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]//生成属性时,按属性内部内容生成(例如在此控件里面(Size-Height,Size_Width))
//[PersistenceMode(PersistenceMode.InnerProperty)]//以子标签的形式显示(例如<Size Width="" Height=""/>)
public Size Size
{
get
{
if (ViewState["Size"]==null) {
ViewState["Size"]=new Size();
}
return (Size)ViewState["Size"];
}
set
{
ViewState["Size"]=value;
}
}
//定义控件的标签形式
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Input;
}
}
//初始化
protected override void OnInit(EventArgs e)
{
this.Style.Add("width", Size.Width + "px");
this.Style.Add("height", Size.Height + "px");
this.Attributes.Add("type", "submit"); //提交按钮
this.Attributes.Add("value",Text);
this.Attributes.Add("name",this.UniqueID);//回发事件必须有的一个属性
base.OnInit(e);
}
//打印当前控件的内容
protected override void RenderContents(HtmlTextWriter output)
{
//output.Write(Text);
}
public delegate void ClickHandle();
private object key=new object();
public event ClickHandle Click {
add {
this.Events.AddHandler(key,value);
}
remove {
this.Events.RemoveHandler(key, value);
}
}
//按钮的回发事件
public void RaisePostBackEvent(string eventArgument)
{
ClickHandle handle=(ClickHandle)base.Events[key];
if (handle !=null) {
handle();
}
}
}
}
关于ASP.NET自定义Web服务器控件之Button控件的用户互动如下:
相关问题:asp.net C# 如果将Control类强制转换成其它服务器...
答:进行显示转换 foreach(Control ctrol in this.panel1.Controls ) { if ( ctrol.GetType().ToString() == "System.Windows.Forms.TextBox" ) ) { TextBox txt1=(TextBox)ctrol ; } else if(control is System.Windows.Forms.Button) { // control... >>详细
相关问题:设置自定义ASP.NET服务器控件TagPrefix的几种方法...
答:1usingSystem;2usingSystem.Collections.Generic;3usingSystem.ComponentModel;4usingSystem.Text;5usingSystem.Web;6usingSystem.Web.UI;7usingSystem.Web.UI.WebControls;89namespaceServerControl10{11[ToolboxData(" >>详细
相关问题:asp.net 急需web版的button控件60秒倒计时c#语言
答:用ajax来实现吧 // >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【ASP】ASP.NET中内嵌页面代码的一个问题-NET-内
- 【As】Asp.net中的页面乱码的问题-sp--pn-ne-et
- 【增加记录】asp.net中获取新增加记录的ID Access
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【404页面】ASP.NET设置404页面返回302HTTP状态码
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
