The example in this article describes the implementation method for the button control of the ASP.net custom Web server control. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
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;
Customizing the Web server 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)]//generates properties by its internal content, such as inside this control ( Size-height,size_width))
[PersistenceMode (Persistencemode.innerproperty)]//displayed as a child label (for example, <size width= "height=" "/>")
Public size Size
{
Get
{
if (viewstate["Size"] = = null) {
viewstate["Size" = new size ();
}
return (size) viewstate["size"];
}
Set
{
viewstate["Size"] = value;
}
}
Define the label form of a control
protected override HtmlTextWriterTag Tagkey
{
Get
{
return htmltextwritertag.input;
}
}
Class
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"); Submit button
This. Attributes.Add ("value", Text);
This. Attributes.Add ("name", this.) UniqueID);/postback event must have a property
Base. OnInit (e);
}
Print the contents of the current control
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);
}
}
Postback Events for buttons
public void RaisePostBackEvent (String eventargument)
{
Clickhandle handle = (Clickhandle) base. Events[key];
if (handle!= null) {
Handle ();
}
}
}
}
Copy Code code as follows:
<%@ 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 ">
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<!--custom Server button control-->
<cc1:mybutton id= "MyButton1" size-height= "size-width=" 290 "onclick=" btnsubmit "text=" I am a separate submit button (custom server) " runat= "Server"/>
</div>
</form>
</body>
Copy Code code as follows:
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)
{
}
Customizing server controls
protected void Btnsubmit () {
Response.Write ("I am the Click event for custom server Controls");
}
}
I hope this article will help you with the ASP.net program design.