Custom webpart - best approach

Vadlamani . asked on September 30, 2020 21:48

Hi Team, I am little bit confused on the approach to follow for creation of custom webpart. 1) when should I go for layout (webpart's layout tab). 2) when to use code behind.

What is the difference between the two approach?

Recent Answers


Vadlamani . answered on October 1, 2020 09:19

Any thoughts?

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on October 1, 2020 09:20 (last edited on October 1, 2020 09:21)

Hi,

Depends mainly on what you want to customize on the Web Part and what it actually does.

Is it a static HTML Web Part where you want to add an additional Class on a DIV. Or do you want to update the BizzForm to Validate input with an external service and directly insert it into an external CRM.

Adding a different Layout is mainly for applying a different Skin when you replace some HTML markup. You still need the same controls as the backend code relies on it. (I always find it hard to locate such changes, e.g. when you update the Web Part later you might forget the alternative Layouts.)

Changing the code behind actually changes the inner logic for ALL usages of this web part, as well as any derived Widgets.

I would usually recommend to not Change any active Web Part or one that comes out of the box from Kentico. Rather I'd clone it as a new one. This way you can change the behavior specific to this needs, and clean out everything you don't need.

0 votesVote for this answer Mark as a Correct answer

Vadlamani . answered on October 2, 2020 11:35

Thanks for your response.

Say I have created a new webpart, added few fields in properties then accessing the newly created fields/properties in the HTML markup. The HTML markup are placed in Layouts tab and not in code behind.

After which we will be converting it to Widgets and we will reuse the widgets in our pages (single instance or multiple instances).

Questions: 1) Will there be any cons in using this approach, say incase of upgrading to newer version of kentico? 2) Instead of using Layouts configured , can we use code behind? Any difference in using this approach? 3) Incase I need to do some validation on fields and display the element in HTML, can I go with the Layouts approach ?

Please suggest.

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on October 2, 2020 11:54

Hi,

  1. If you clone a Web Part it is completely isolated from other controls. So in case anything changes due to a hotfix or upgrade it will not change your new version. Down side is that in case API changes or a security flaw is resolved you of course need to upgrade it manually...

  2. The "Default" layouts in Kentico is tied to the code before (.ASCX) on disk. You can view all current "layouts" on disk. E.g. the StaticHTML web part has a code before (with the markup) stored at: CMS\CMSWebParts\Text\staticHTML.ascx the code behind contains the logical code and is stored on same location as staticHTML.ascx.cs. If you add an additional Layout in Kentico you're basically overwriting the .ascx file with a version that is stored in the Kentico Admin (so directly in the Database).

  3. I'm still not sure what type of Web Part you want to build? But if you want to make changes I'd still recommend to create a dedicated clone!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.