ASP. NET 2.0 custom control development (I) page rendering

Source: Internet
Author: User

To write a custom control, you must first select the base class:

1. All standard controls can be used as the base class. You can choose the standard control you need to override the new control.

2. If you cannot find the appropriate base class from the standard control, you can use one of the following three

A) system. Web. UI. Control (base classes of all ASP. NET controls)

B) system. Web. UI. webcontrols. webcontrol (inherited from)

C) system. Web. UI. webcontrols. compositecontrol (inherited from B)

I. display method of control

Control is implemented by overwriting its render () method.

Render method declaration:

Code
Protected InternalVitrualVoidRender (htmltextwrite output)
{
//Todo
}

The protected and virtual attributes must exist. Therefore, the class inherited from control can override the render method to implement its own rendering logic.

The attribute internal allows this method to be called only in the ASP. NET Framework.Code.

 

Code
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Web. UI;

Namespace Ikecontrol
{
Public   Class Logintex: Control
{
Protected   Override   Void Render (htmltextwriter writer)
{
// Start the DIV of the outermost layer
Writer. renderbegintag (htmltextwritertag. Div );
// Add text
Writer. writeline ( " Username & nbsp; " );
// Add username logon box style and attributes
Writer. addattriter( htmltextwriterattribute. type, " Text " );
Writer. addstyleattribute (htmltextwriterstyle. Width, " 100px " );
Writer. addstyleattribute (htmltextwriterstyle. fontsize, " 12px " );
Writer. addstyleattribute (htmltextwriterstyle. color, " 0000ff " );
Writer. addstyleattribute (htmltextwriterstyle. textdecoration, " Blink " );

// Start username logon box
Writer. renderbegintag (htmltextwritertag. Input );
// Add line feed characters
Writer. renderbegintag (htmltextwritertag. BR );
// End line feed symbol
Writer. renderendtag ();
// Add Password text
Writer. writeline ( " Password & nbsp; Code & nbsp; " );
// End User Name logon box
Writer. renderendtag ();
// Add Password Logon box style and attributes
Writer. addattriter( htmltextwriterattribute. type, " Password " );
Writer. addstyleattribute (htmltextwriterstyle. fontsize, " 12px " );
Writer. addstyleattribute (htmltextwriterstyle. backgroundcolor, " # Ff00ff " );
// Start Password Logon box
Writer. renderbegintag (htmltextwritertag. Input );
// End Password Logon box
Writer. renderendtag ();
// End the outermost Div
Writer. renderendtag ();
}
}
}

1. When we add him to the page, the IDE will automatically add such a code

Code

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> <% @ register Assembly = " ikecontrol " namespace = " ikecontrol " tagprefix = " Ike " %>

The tagprefix attribute declares the control prefix, And the IDE defaults to CCL. Of course, you can define it as your favorite. (It is best not to change it to ASP to avoid confusion with standard controls)

Custom tagprefix has three methods:

1) modify it on the ASPX page. This will only affect the current page. The entire project cannot be modified.

2) You must modify the entire project in the web. config file. Add the following code under system. Web.

Code
< Pages >
< Controls >
< Add assembly = " Ikecontrol "   Namespace = " Ikecontrol " Tagprefix = " Ike " />
</ Controls >
</ Pages >

3) assemblyinfo. CS and

Code
UsingSystem. Web. UI;
UsingSystem. componentmodel;
[Assembly: tagprefix ("Logintext","Ike")]

Tagprefix ("namespace", "control prefix ")

 

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.