Analysis of ASP. NET control design support

Source: Internet
Author: User

What are ASP. NET controls supported during design? 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.

ASP. NET controls are designed to support the 1. 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.

ASP. NET controls are supported during design II. HTML

(1) define controls

Let's define two simplest controls

 
 
  1. public class DesginControl : Control  
  2.   {  
  3.  
  4.       public string Text  
  5.       {  
  6.           get 
  7.           {  
  8.               String s = (String)ViewState["Text"];  
  9.               return ((s == null) ? String.Empty : s);  
  10.           }  
  11.  
  12.           set 
  13.           {  
  14.               ViewState["Text"] = value;  
  15.           }  
  16.       }  
  17.       protected override void Render(HtmlTextWriter writer)  
  18.       {  
  19.           writer.Write(Text);  
  20.       }  
  21.   }  
  22.  
  23.   public class DesginWebControl :WebControl  
  24.   {  
  25.       public string Text  
  26.       {  
  27.           get 
  28.           {  
  29.               String s = (String)ViewState["Text"];  
  30.               return ((s == null) ? String.Empty : s);  
  31.           }  
  32.  
  33.           set 
  34.           {  
  35.               ViewState["Text"] = value;  
  36.           }  
  37.       }  
  38.       protected override void RenderContents(HtmlTextWriter writer)  
  39.       {  
  40.           writer.Write(Text);  
  41.       }  
  42.   } 

(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.

 

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

Next we will add the Text attribute to the two of them, and then let's look at the effect.

 

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 try again to remove the content of the Text attribute.

The effect is as follows:

 

The DesignControl is restored. What's worse is that the 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 support during design. Otherwise, I am afraid you will not be able to access several interfaces above. fortunately, we can change it by ourselves. that is to use.. 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 attribute is blank, the designer is displayed as "[" add control ID value "]", for example, [Label1]. Note that this is the rendering of the designer, instead of generating 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.

 
 
  1. Public ClassDesginWebControlDesigner: ControlDesigner
  2. {
  3. PrivateDesginWebControl webControl;
  4.  
  5. PublicDesginWebControlDesigner ()
  6. {}
  7.  
  8. // Initialize the control designer 
  9. Public Override VoidInitialize (IComponent ponent)
  10. {
  11. Base. Initialize (ponent );
  12.  
  13.  
  14. WebControl = (DesginWebControl) ponent;
  15. WebControl. Text ="DesginWebControl";
  16. }
  17.  
  18. // Adjust the widget size 
  19. Public Override BoolAllowResize
  20. {
  21. Get 
  22. {
  23. Return False;
  24. }
  25. }
  26.  
  27. // Obtain the designer HTML 
  28. Public Override StringGetDesignTimeHtml ()
  29. {
  30. If(WebControl. Text. Length> 0)
  31. {
  32. StringSpec ="<Font color = 'red'> {0} </font>";
  33. ReturnString. Format (spec, webControl. Text );
  34. }
  35. Else 
  36. ReturnGetEmptyDesignTimeHtml ();
  37. }
  38.  
  39. // Define an empty implementation 
  40. Protected Override StringGetEmptyDesignTimeHtml ()
  41. {
  42. StringSpec ="[{0}]";
  43. ReturnString. Format (spec, webControl. ID );
  44. }
  45. }

(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.

 
 
  1. [Designer(typeof(DesginWebControlDesigner))]  
  2. public class DesginWebControl :WebControl  
  3. {  

Now, you can test it.

 

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.

This article introduces the support for ASP. NET controls during design. I hope to help you understand the support for ASP. NET controls during design.

  1. Server Control client Function Based on ASP. NET control development
  2. Analysis on control generator of ASP. NET Control Development
  3. Summary of ASP. NET control development
  4. Development of ASP. NET template controls
  5. Development of ASP. NET data binding controls

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.