ASP.NET自定義Web服務(wù)器控件之Button控件
本文實(shí)例講述了ASP.NET自定義Web服務(wù)器控件之Button控件實(shí)現(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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服務(wù)器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)]//生成屬性時,按屬性內(nèi)部內(nèi)容生成(例如在此控件里面(Size-Height,Size_Width))
//[PersistenceMode(PersistenceMode.InnerProperty)]//以子標(biāo)簽的形式顯示(例如<Size Width="" Height=""/>)
public Size Size
{
get
{
if (ViewState["Size"] == null) {
ViewState["Size"] = new Size();
}
return (Size)ViewState["Size"];
}
set
{
ViewState["Size"] = value;
}
}
//定義控件的標(biāo)簽形式
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);//回發(fā)事件必須有的一個屬性
base.OnInit(e);
}
//打印當(dāng)前控件的內(nèi)容
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);
}
}
//按鈕的回發(fā)事件
public void RaisePostBackEvent(string eventArgument)
{
ClickHandle handle = (ClickHandle)base.Events[key];
if (handle != null) {
handle();
}
}
}
}
<%@ 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>
<!--自定義服務(wù)器按鈕控件-->
<cc1:MyButton ID="MyButton1" Size-Height="30" Size-Width="290" OnClick="btnSubmit" Text="我是一個單獨(dú)的提交按鈕(自定義服務(wù)器)" runat="server" />
</div>
</form>
</body>
</html>
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)
{
}
//自定義服務(wù)器控件
protected void btnSubmit() {
Response.Write("我是自定義服務(wù)器控件的點(diǎn)擊事件");
}
}
希望本文所述對大家的asp.net程序設(shè)計(jì)有所幫助。
- ASP.NET服務(wù)器端控件RadioButtonList,DropDownList,CheckBoxList的取值、賦值用法
- asp.net Page.EnableEventValidation 屬性驗(yàn)證服務(wù)器控件的回發(fā)和回調(diào)事件出現(xiàn)的錯誤
- jquery獲取ASP.NET服務(wù)器端控件dropdownlist和radiobuttonlist生成客戶端HTML標(biāo)簽后的value和text值
- asp.net 服務(wù)器控件的 ID,ClientID,UniqueID 的區(qū)別
- asp.net下使用Request.From獲取非服務(wù)器控件的值的方法
- jQuery生成asp.net服務(wù)器控件的代碼
- ASP.NET 動態(tài)寫入服務(wù)器端控件
- asp.net Page.Controls對象(找到所有服務(wù)器控件)
- Asp.Net使用服務(wù)器控件Image/ImageButton顯示本地圖片的方法
相關(guān)文章
ASP.NET Core 7 Razor Pages項(xiàng)目發(fā)布到IIS的詳細(xì)過程
這篇文章主要介紹了ASP.NET Core 7 Razor Pages項(xiàng)目發(fā)布到IIS的詳細(xì)過程,詳細(xì)介紹了發(fā)布過程遇到的問題及解決方法,對ASP.NET Core 發(fā)布到IIS相關(guān)知識感興趣的朋友一起看看吧2023-01-01關(guān)于.NET6?Minimal?API的使用方式詳解
本文我們主要是介紹了ASP.NET?Core?6?Minimal?API的常用的使用方式,在.NET6中也是默認(rèn)的項(xiàng)目方式,整體來說卻是非常的簡單、簡潔、強(qiáng)大、靈活,不得不說Minimal?API卻是在很多場景都非常適用的2021-12-12.net core高吞吐遠(yuǎn)程方法如何調(diào)用組件XRPC詳解
這篇文章主要給大家介紹了關(guān)于.net core高吞吐遠(yuǎn)程方法如何調(diào)用組件XRPC的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用.net core具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Asp.Net實(shí)現(xiàn)的通用分頁函數(shù)
這篇文章主要介紹了Asp.Net實(shí)現(xiàn)的通用分頁函數(shù),結(jié)合實(shí)例形勢分析了asp.net分頁函數(shù)的功能,定義及使用技巧,需要的朋友可以參考下2016-04-04詳解ASP.NET Core MVC 源碼學(xué)習(xí):Routing 路由
本篇文章主要介紹了詳解ASP.NET Core MVC 源碼學(xué)習(xí):Routing 路由 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03aspx超強(qiáng)木馬查殺與防范(web網(wǎng)馬)
下面代碼是一個aspx超強(qiáng)木馬,功能很多,大家在服務(wù)器上見到一定要小心2013-12-12Entity Framework加載控制Loading Entities
本文詳細(xì)講解了Entity Framework加載控制Loading Entities的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03