Asp. NET server Control programming warm-up campaign

Source: Internet
Author: User
Tags object model

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.");
}
 }
}

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.