When to use CreateChildControls () versus ASPX injection

I am developing a webpart for SharePoint 2007 and I see a few posts that advise you to do all the creation of controls in your code. I am moving from developing Java to J2EE, so I don't have a history of the .Net / ASP / platform, etc.

In other places, it shows how you can do the same by embedding a control definition in an asp page with tags

My question is:

What is the rule governing where to exercise control? Recently, this rule has changed, perhaps ASP vs ASP.Net or ASP.Net MVC? Is this tip limited to SharePoint development?

+4
source share
2 answers

I am going to exclude the fact that you are developing a web part for 2007. Web Parts mean that you want to give users the ability to add details to the pages of their choice. To implement the web part, you will need a separate DLL, which is either deployed in the bin directory of a SharePoint IIS application or the global build cache.

Typically, with SharePoint you do not have an ASPX page to insert controls inside and make it reproducible. ASPX pages will be created by users or features, and users will then add web parts to these pages. SharePoint is actually a virtual file system, and many of the ASPX pages either live in the database or in the SharePoint root folder, creating code for a non-trivial task.

The web part must be deployed as a separate DLL file. Given the way web parts are designed, you don’t have a design surface for creating a user interface, so you need to create your control tree using code, and the CreateChildControls section is the preferred time in the life cycle (see the previous link).

There are other ways, such as Son of SmartPart ( http://weblogs.asp.net/jan/archive/2005/11/22/431151.aspx ), which allows you to create a user control and then you load the user control into web part management tree. This is essentially the Microsoft approach adopted in 2010 with their Visual Web Part functionality.

The vast majority of web part development for SharePoint uses the same namespace as for ASP.NET, so if you are not performing certain SharePoint functions in your code, such as reading a list, etc., web parts should be used in ASP.NET and SharePoint.

John

+2
source

here are a few thoughts.

Creating dynamic controls (inside CreateChildControls ()

  • this is a more dynamic solution
  • If the control layout should be different, for example, for different users, you have more control over the flow. This means that you can insert controls into different trees or branches of the tree, and just make them visible / invisible.
  • may be more appropriate if you need to make management instances accessible from outside the assembly

Declarative Definition (ASPX)

  • Actually more MVC style.
  • This is the recommended method if you have no reason (perhaps for the above reasons)
  • MORE easier to maintain UI components and layout
  • you can also make reusable user controls in a declarative way by creating ASCX files.

You can also mix and match. Declare some static elements and dynamically sprinkle them with others, if necessary, adding them inside the Panel control at run time.

another common approach is to subcompose common elements (such as a TextBox or data grid) and change the way you control, view, and / or render this control (by overriding the Render event). Than you can just use your version of the control inside ASPX. This is a win / win because you get the most control while still separating the layout from the business logic. I am always a subclass to implement common functions, such as the uniform appearance of all controls, personalization, etc.

+1
source

Source: https://habr.com/ru/post/1311742/


All Articles