ASP.NET自定义Web服务器控件之Button控件
程序员文章站
2024-02-23 08:59:58
本文实例讲述了asp.net自定义web服务器控件之button控件实现方法。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:using system;&n...
本文实例讲述了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();
}
}
}
}
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();
}
}
}
}
复制代码 代码如下:
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<%@ register assembly="mycontrols" namespace="mycontrols" tagprefix="cc1" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<!--自定义服务器按钮控件-->
<cc1:mybutton id="mybutton1" size-height="30" size-width="290" onclick="btnsubmit" text="我是一个单独的提交按钮(自定义服务器)" runat="server" />
</div>
</form>
</body>
</html>
<%@ register assembly="mycontrols" namespace="mycontrols" tagprefix="cc1" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<!--自定义服务器按钮控件-->
<cc1:mybutton id="mybutton1" size-height="30" size-width="290" onclick="btnsubmit" text="我是一个单独的提交按钮(自定义服务器)" runat="server" />
</div>
</form>
</body>
</html>
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
//自定义服务器控件
protected void btnsubmit() {
response.write("我是自定义服务器控件的点击事件");
}
}
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
//自定义服务器控件
protected void btnsubmit() {
response.write("我是自定义服务器控件的点击事件");
}
}
希望本文所述对大家的asp.net程序设计有所帮助。
上一篇: java版简单的猜数字游戏实例代码
下一篇: Python算法应用实战之队列详解