ASP.NET Custom Controls - Composites

后端 未结 6 518
别跟我提以往
别跟我提以往 2020-12-13 20:13

Summary

Hi All,
OK, further into my adventures with custom controls...

In summary, here is that I have learned of three main "classes" of cus

6条回答
  •  温柔的废话
    2020-12-13 20:47

    Using custom composite controls has a point in a situation where you have a large web application and want to reuse large chunks in many places. Then you would only add child controls of the ones you are developing instead of repeating yourself. On a large project I've worked recently what we did is the following:

    • Every composite control has a container. Used as a wrapped for everything inside the control.
    • Every composite control has a template. An ascx file (without the <%Control%> directive) which only contains the markup for the template.
    • The container (being a control in itself) is initialized from the template.
    • The container exposes properties for all other controls in the template.
    • You only use this.Controls.Add([the_container]) in your composite control.

    In fact you need a base class that would take care of initializing a container with the specified template and also throw exceptions when a control is not found in the template. Of course this is likely to be an overkill in a small application. If you don't have reused code and markup and only want to write simple controls, you're better off using User Controls.

提交回复
热议问题