ASP. NET custom Web server control Button control, asp. netbutton

Source: Internet
Author: User

ASP. NET custom Web server control Button control, asp. netbutton

This article describes how to implement the Button control of ASP. NET custom Web server control. Share it with you for your reference. The specific implementation method is as follows:

Copy codeThe Code is 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;

// Customize 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)] // generate an Attribute Based on the internal Content of the attribute (for example, in this control (Size-Height, Size_Width ))
// [PersistenceMode (PersistenceMode. InnerProperty)] // It is displayed as a sub-tag (for example, <Size Width = "" Height = ""/>)
Public Size
{
Get
{
If (ViewState ["Size"] = null ){
ViewState ["Size"] = new Size ();
}
Return (Size) ViewState ["Size"];
}

Set
{
ViewState ["Size"] = value;
}
}
// Define the label form of the control
Protected override HtmlTextWriterTag TagKey
{
Get
{
Return HtmlTextWriterTag. Input;
}
}

// Initialization
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); // an attribute required for the send-back event
Base. OnInit (e );
}
// Print the content 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 );
}
}
// Button sending back event
Public void RaisePostBackEvent (string eventArgument)
{
ClickHandle handle = (ClickHandle) base. Events [key];
If (handle! = Null ){
Handle ();
}
}
}
}

Copy codeThe Code is as follows: <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>

<% @ Register assembly = "MyControls" namespace = "MyControls" tagprefix = "PC3" %>

<! 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>
<! -- Custom server button control -->
<PC3: myButton ID = "MyButton1" Size-Height = "30" Size-Width = "290" OnClick = "btnSubmit" Text = "I Am a separate submit button (custom server) "runat =" server "/>
</Div>


</Form>

</Body>
</Html>

Copy codeThe Code is 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)
{

}
// Custom server controls
Protected void btnSubmit (){
Response. Write ("I Am a click event of a custom Server Control ");
}
}

I hope this article will help you design your asp.net program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.