There are several ways to create a server control in ASP.net and a control for Windows Form:
1. User Control
2. Custom controls derived from control and WebControl
3, from existing ASP.net server control extensions
The user control has an. ascx extension, and save as a text file, the user control does not need to be precompiled as a server control derived from controls and WebControl, and when the user control is used in the. aspx page, the page parser dynamically generates a class from the. aspx file and compiles it to an assembly Pieces. The advantages are: To solve the code reuse, while each user control has its own object model, its written language and the language of the. aspx page.
Extending from existing ASP.net server controls is mainly about the functionality of. NET native server controls to suit our development and end user needs.
Custom controls derived from control and WebControl are deployed in the form of a compiled class library.
The above 1 and 3 are not explained in this series, and only the server controls derived from control and WebControl are explained in this series.
We're going to write a custom control that, as long as it's inherited from controls, WebControl, and IComponent interfaces, and WebControl itself is derived from control, so they also support the visual design of the component.
Render method and HtmlTextWriter class, When we derive a ASP.net server control from one of the controls, our control class provides us with an overloaded render and an instance of a HtmlTextWriter type, render by sending the contents of server controls to the provided HtmlTextWriter object, while HtmlTextWriter encapsulates the functional function of the HTML write text stream.
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary1
{
public class Control1 : System.Web.UI.Control
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.Write("I'm here.");
}
}
public class Control2 : System.Web.UI.WebControls.WebControl
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.Write("I'm here too.");
}
}
}