Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > Add webpart to page programatically View modes: 
User avatar
Member
Member
davidt4n-gmail - 2/5/2014 9:28:03 AM
   
Add webpart to page programatically
Hi,

I would like to know if there's a way of adding a web part to a page without using Kentico and without adding it manually on the design tab. The web part is already created.

I want to be able to write one or a few lines of codes to display the webpart. Similar to a Javascript widget (e.g: document.write(showWebPart("mywebpart")). This way, the webpart can be reuse from any website. I own a few websites that aren't integrated with Kentico, but I would like to be able to use that webpart made in Kentico.

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 2/6/2014 6:38:39 AM
   
RE:Add webpart to page programatically
Then your best bet would be to simply create them as user controls and load them dynamically. Kentico has a user control webpart that you can simply write standard asp.net ascx controls and place them on a page without a problem. Keep in mind if you plan to use any of the Kentico namespaces you'll need those dll's within your project.

User avatar
Member
Member
davidt4n-gmail - 2/6/2014 9:29:57 AM
   
RE:Add webpart to page programatically
When I created my webpart, it generated a Web User Control. This is the webpart I wanna reuse in other websites (Let's say its completed). How exactly am I suppose to proceed to get that webpart into a website (this website is not integrated in Kentico). I probably have to import something to be able to reuse the webpart.

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 2/6/2014 11:39:58 AM
   
RE:Add webpart to page programatically
Let me step back a bit and say, this is not Kentico related at all but more of a standard ASP.NET question, correct? If so you may have better luck getting what you need off of the Stackoverflow website or asp.net website.

So say you have 3 projects you're working on:
1) Kentico website.
2) Standard asp.net website.
3) Website where you're building your user controls.

I'm assuming you know that your user control has to be generic enough to not have any dependencies on other projects if you plan to use it across different asp.net based websites (Kentico, DNN, standard, etc).

What I was saying in my initial post is Kentico has a web part already created that will do all the dynamic work for you. You simply copy your generic user control you created (#3) and place it in the Kentico file system (#1) and place the User Control webpart on a page within the content tree. In the webpart you set the location to that user control, which is typically something like /TheSiteCodeName/controls/usercontrol.ascx or wherever you copied that user control and click save. That's it.

In a standard asp.net website (#2) you will have to copy the user control to the websites file system and in Visual Studio you can click and drag the user control to the design part of the page and it will create a reference for you.
http://msdn.microsoft.com/en-us/library/5d0t5fak(v=vs.100).aspx

I'd suggest taking a look at the examples for dynamically adding user controls to pages if you don't want to use the drag and drop method.
http://msdn.microsoft.com/en-us/library/c0az2h86(v=vs.100).aspx
http://stackoverflow.com/questions/4302130/loading-user-controls-dynamically