Learning custom web control programming

Source: Internet
Author: User

To write a custom web control, you must inherit system. Web. UI. Control and rewrite the render method to render the control. No matter how complicated a web control is, its output is the most basic HTML element. Therefore, the key to rewriting render is to convert the web control into HTML elements. The control class has a read/write ID attribute that uniquely identifies an object on the server. The control class also has two important attributes: clientid and uniqueid. The former outputs the ID attribute of the HTML element, and the latter outputs the name attribute of the HTML element. The following describes a custom Textbox, which implements the text attribute:
Public class customtextbox: Control
{
Private string _ text;
Public String text
{
Set {This. _ text = value ;}
Get {return this. _ text ;}
}

Protected override void render (htmltextwriter writer)
{
Writer. writebegintag ("input ");
Writer. writeattribute ("name", this. uniqueid );

If (this. ID! = NULL)
Writer. writeattribute ("ID", this. clientid );

Writer. writeattribute ("value", this. Text );

Writer. Write (htmltextwriter. tagrightchar );
}
}
The render method is called by Asp.net. The htmltextwriter object is passed in by the system and HTML code is output in an object-oriented way.
There are two problems to solve: one is how to update the text attribute after sending back; the other is how to implement the textchanged event of the Textbox Control. This needs to be achieved by implementing the loadpostdata () method of ipostbackdatahandler and the raisepostdatachangedevent () method. Both methods are called by Asp.net. The former is used to read the data that the user inputs to the HTML element on the client. If loaspostdata () returns true, the system calls raisepostdatachangedevent (). The added code is as follows:
Public bool loadpostdata (string postdatakey,
Namevaluecollection postcollection)
{
String presentvalue = This. text;
This. Text = postcollection [postdatakey];
Return (presentvalue! = This. Text );
}

Public event eventhandler textchanged;

Public void raisepostdatachangedevent ()
{
If (this. textchanged! = NULL)
This. textchanged (this, new eventargs ());
}
Can the above Code really trigger the textchanged event? Yes. Certainly, even if the user does not make any changes to the value of the HTML element, the textchanged event will be triggered after the return!
Why? Because HTTP is stateless, the HTTP server is faced with access requests from several clients, which cannot be saved for the continuous access of the same customer. For example, Customer A sets variable A to 1 during the first access, and the value of a should be the initial value instead of 1 during the first access. What should we do? The answer is to use viewstate. Viewstate is a mechanism provided by Asp.net to return data between the Web server and the client. The data stored in viewstate is saved to the client. When the client returns the data to the server, the new data and historical data can be compared:
Public String text
{
Set {This. viewstate ["mytext"] = value ;}
Get {return (string) This. viewstate ["mytext"];}
} Protected bool _ autopostback = false;

Public bool autopostback
{
Set {This. _ autopostback = value ;}
Get {return this. _ autopostback ;}
}
The last task is to implement the autopostback attribute. Asp.net provides a method for page. getpostbackeventreference (), which returns a string that represents the client event. It is actually some client code, as long as the following code is inserted in the render method, a textbox is basically complete!

If (this. autopostback)
Writer. writeattribute ("onchange", "javascript:" +
Page. getpostbackeventreference (this ));

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.