Adding custom code to your website

The easiest way to insert custom code into a portal engine based website is using standard ASCX user controls. If you are not familiar with Visual Studio development, you can skip this page.

Current time example

In this example, we will create a simple user control (ASCX) using Visual Studio and integrate it into the Home page.

  1. Open the website project using the WebSite.sln or WebApp.sln file, which is located in the folder where you installed the website.

  2. Create a Web User Control and set its name to GetTime.ascx.You can set the programming language option to either Visual C# or Visual Basic.

  3. Drag the following ASP.NET controls onto the page from the toolbox and set their properties:

    • Button control:

      • ID: Button1
      • Text: Get time
    • Label control:

      • ID: Label1
      • Text: <clear the value>
  4. Switch to the Design view and double-click the Show current time button.

    • The user control’s code behind file opens and creates the Button1_Click method.
  5. Enter the following code into the Button1_Click method:

    
    
    
     Label1.Text = DateTime.Now.ToString();
    
    
     
  6. Save the user control’s files.

The code ensures that the label displays the current date and time when the button is clicked. You do not need to build the project — user controls are compiled at run time.

Adding the user control onto the page

  1. Log in to the Kentico administration interface and open the Pages application.
  2. Select the Home page and switch to the Design tab.
  3. Adda User control web part to the Main zone.
  4. Enter the following value into the web part’s User control virtual path property: ~/GetTime.ascx. The ~ character represents the root of your web application.
  5. Click Save & Close.

Switch to Preview mode to see the user control’s output on the page. When you click the Get time button, the current date and time appears next to the button.

User controls versus web parts

You can also insert custom code onto portal pages by creating your own web parts. Web parts are very similar to user controls, but with a built-in portal engine configuration interface. We recommend building web parts if you need easily re-usable and configurable user controls.

For more information, see Developing web parts in the main documentation.