Adding custom code to your web site

  Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic! Mail us feedback on this topic!  

The easiest way how to insert custom code into the portal engine-based web site is using the standard ASCX user controls. This chapter will show you how to do that. If you're not familiar with Visual Studio development, you can skip this chapter.

 

Current time example

 

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

 

Open the web site project using the WebProject.sln file that is placed in the folder where you deployed the web site. Right-click the web project root in the Solution Explorer window and click the Add new item... option. Choose to create a new Web User Control and set its name to GetTime.ascx. You can set the programming language option to either Visual C# or Visual Basic.

 

tutorial_clip0065

 

Click Add. Switch to the Design tab and drag and drop the following controls and set their properties:

 

Button control:

- ID: Button1

- Text: Get time

 

Label control:

- ID: Label1

- Text: <clear the value>

 

tutorial_clip0066

 

Double-click the Get time button and enter the following code to the Button1_Click method:

 

[C#]

 

Label1.Text = DateTime.Now.ToString();

 

[VB.NET]

 

Label1.Text = DateTime.Now.ToString()

 

This code ensures that the label displays current date and time when the button is clicked. It's not necessary to compile the project - the user controls are compiled at run time.

 

Save both the ASCX and ASCX.CS files.

 

 

Adding the user control on the page

 

Sign in to Kentico CMS Desk, click the Home page and click Design. Add web part of type General -> User control to the left zone. Choose to add the General/User control web part. Enter the following value in the User control virtual path property:

 

~/GetTime.ascx

 

The ~ character represents the root of your web application. Click OK. Click the Live site mode and now you can see the user control inserted into the page. When you click the Get time button now, the current date and time is displayed next to the button:

 

tutorial_clip0067

 

User controls versus web parts

 

Another option how to insert custom code into the page is creating your own web part. A web part is basically also an ASCX user control, but it inherits some standardized properties and methods from the CMSAbstractWebPart class. You will build web parts in case you need to create re-usable, parameterized user controls. The web parts are described in the next chapter.

 

Page url: http://devnet.kentico.com/docs/tutorial/index.html?adding_custom_code_to_your_web_site.htm