Custom button control
Create a class gutton
Using system; using system. data; using system. configuration; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; namespace searchinfo {// <summary> // custom button control gutton /// </Summary> public class gutton: webcontrol, ipostbackeventhandler {Public String text {set {viewstate ["text"] = V Alue;} get {string STR = (string) viewstate ["text"]; If (STR! = NULL) {return STR;} return string. empty ;}} protected override htmltextwritertag tagkey // rewrite the tag. The default value is <span> -- gextbox, which generates the <span> tag {get {return htmltextwritertag. input; // return base. tagkey ;}} protected override void addattributestorender (htmltextwriter writer) // Add attributes for the tag {writer. addattribute (htmltextwriterattribute. name, this. uniqueid); // a property writer that is required for the send-back event. addattribute (htmltextwriterattribu Te. value, this. text); // writer. addattribute (htmltextwriterattribute. type, "button"); writer. addattribute (htmltextwriterattribute. type, "Submit"); // submit enables submission. The ASP Server Control is also the generated submit // base. addattributestorender (writer);} protected override void rendercontents (htmltextwriter writer) // content in the tag {// base. rendercontents (writer);} public event eventhandler click; # region ipostbackeventhandler member public Vo Id raisepostbackevent (string eventargument) {If (Click! = NULL) {Click (this, eventargs. Empty); // parameter 1: Event sender. Parameter 2: event information. }}# Endregion }}
Create gutton. aspx
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "gutton. aspx. CS "inherits =" gutton "%> <% @ register namespace =" searchinfo "tagprefix =" cc "%> <! 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">
Background page gutton. aspx. CS
Using system; using system. data; using system. configuration; using system. collections; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; public partial class gutton: system. web. UI. page {protected void page_load (Object sender, eventargs e) {} protected void gutton_onclick (Object sende R, eventargs e) {response. Write ("click the button! ");}}