Supported Asp.net controls during design (1)

Source: Internet
Author: User
It seems that there is not much discussion about the control design. Is it possible to implement functions? Of course, the support during design is dispensable. I dare say that many people do not like it if it is not supported by design. net, because of the design support, more lower. net learning threshold. this time, let's take a look at it. If you simply implement several common functions.

I.Controldesigner class

Controldesigner is the base class of the Web Server Control designer. You can extend this class. after you understand this class, you will find that the controls you normally see can be perfectly presented in the vs2005 design, which is also made by Microsoft. in this case, as a perfect control, it should also be equipped with design support, sometimes it can make development more efficient. the specific methods and attributes of this class are not introduced here. we will use it as needed.

2. Obtain the HTML associated control during design

(1) define controls

Let's define two simplest controls

Public class desgincontrol: Control
{

Public String text
{
Get
{
String S = (string) viewstate ["text"];
Return (S = NULL )? String. Empty: S );
}

Set
{
Viewstate ["text"] = value;
}
}
Protected override void render (htmltextwriter writer)
{
Writer. Write (text );
}
}

Public class desginwebcontrol: webcontrol
{
Public String text
{
Get
{
String S = (string) viewstate ["text"];
Return (S = NULL )? String. Empty: S );
}

Set
{
Viewstate ["text"] = value;
}
}
Protected override void rendercontents (htmltextwriter writer)
{
Writer. Write (text );
}
}

(2) First test

Well, one of the two controls is derived from control, and the other is derived from webcontrol. After compilation, drag them together to the designer to see the effect.


Figure 1

When the text attribute is empty, the designer displays the text above when no content is displayed.

Designwebcontrol is even more out of the way. If you see the small box on the right of the designwebcontrol text, this is the default effect.

(2) second test

BelowAdd text attributes to them againAfter the effect, then look at the effect


Figure 2

Designcontrol has a good display effect,
Designwebcontrol can achieve the same effect as designcontrol, but it has multiple functions.
It allows the automatic Drag Control in the designer to change the width and height of the control, and the 115 and 42 of the control in the middle of the right mouse and control are not displayed. unfortunately, this function is not available in designcontrol. why?

(4) Third test

Let's try again for the last test.We will remove the content of the text attribute again.
The effect is as follows:


Figure 3

The designcontrol is restored.Designwebcontrol becomes a whiteboard

(6) The solution is coming.
In fact, the above two controls are very similar to the literal and label controls, and they do not have any problems in use. They are designed to look better, but also to protect our eyes from being more comfortable :), what can we do.
Everything is because. NET provides design support. Otherwise, I am afraid you will not be able to see several interfaces above. Fortunately, we can change it by ourselves.It is supported by the design of. net.

(7) each control should have its own design support

I don't know if you agree with this sentence. Even if the control may not be needed now, please make preparations later. You can define one with no space first.

Next we will discuss the label control.

7.1 When the text property is blank, the designer is displayed as "[" add control id value "]", as shown in[Label1]. Note that this isThe designer rather than the generated results.
7.2control class does not have the width and height attributes. Of course, it cannot be changed during design.

(8) Implementation

I 've talked a lot of nonsense above. Let's take a look at how to implement it.

Public class desginwebcontroldesigner: controldesigner
{
Private desginwebcontrol webcontrol;

Public desginwebcontroldesigner ()
{}

// Initialize the control designer
Public override void initialize (icomponent ponent)
{
Base. initialize (ponent );


Webcontrol = (desginwebcontrol) ponent;
Webcontrol. Text = "desginwebcontrol ";

}

// Adjust the widget size
Public override bool allowresize
{
Get
{
Return false;
}
}

// Obtain the designer html
Public override string getdesigntimehtml ()
{
If (webcontrol. Text. length> 0)
{
String spec = "<font color = 'red'> {0} </font> ";
Return string. Format (SPEC, webcontrol. Text );
}
Else
Return getemptydesigntimehtml ();
}

// Define an empty implementation
Protected override string getemptydesigntimehtml ()
{
String spec = "[{0}]";
Return string. Format (SPEC, webcontrol. ID );
}
}

(1) The initialize method initializes the status of the control during design.
(2) The default allowresize attribute is true. If it is set to false, the widget cannot be adjusted in the designer.
(3) The getdesigntimehtml method obtains the control's status in the designer.
(4) The getemptydesigntimehtml method defines an empty implementation.

After definition, associate the control with the designer [designer (typeof (desginwebcontroldesigner)]
Public class desginwebcontrol: webcontrol
{
}

Now, you can test it.


Figure 4

The effect is the same as the definition. This is the design effect. Of course, the generated page is not like this. This should be clearly distinguished.
Now let's modify the text attribute and the control's backcolor attribute. We can see that the text has changed, and the backcolor attribute remains unchanged after being changed.

Note:
(1) The getdesigntimehtml method presents the final effect of the control designer. You cannot change the effect defined in this method during use. The initialization effect of the initialize method can be changed.
(2) The page rendering effect is not necessarily the same as that of the designer.

After talking about so many things, you can understand the most basic things and things that are easy to understand.

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.