Kentico CMS 7.0 Developer's Guide

Adding custom code to web parts (obsolete)

Adding custom code to web parts (obsolete)

Previous topic Next topic Mail us feedback on this topic!  

Adding custom code to web parts (obsolete)

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

 

InfoBox_Exclamation

 

Obsolete feature

 

This feature is now obsolete. If you need to customize the behavior of some web part, please clone the web part and customize it.

 

If you still need to use this feature (for backward compatibility), you need to enable it by adding the following parameter to your web.config file, to the appSettings section:

 

<add key="CMSShowWebPartCodeTab" value="true" />

 

 

InfoBox_Note

 

Limitations when using the Code tab

 

If you add custom code on the Code tab, you will not be able to pre-compile the website and you will not be able to use the website in a medium trust environment. If this is an issue for you, you can use the alternative solutions described in this chapter.

 

If you need to modify the behavior or enhance the functionality of some web part only on a single page template, you can add custom code on the Code tab:

 

devguide_clip0811

 

The following code displays the current date and time below the current web part:
 

[C#]

 

<asp:Label runat="server" id="lblTime"></asp:Label>

<script runat="server">

  protected override void OnLoad(EventArgs e)

   {

       base.OnLoad(e);

       this.lblTime.Text = "The time is: " + DateTime.Now.ToString();

   }

</script>

 

 

InfoBox_Note

 

Programming language

 

Please note: You can only use the programming language in which the web part is written. If the web part was written in C#, you can use only C# here.

 

 

The following code sample sets the WhereCondition property of the web part dynamically at run-time:

 

[C#]

 

<script runat="server">

public override void OnContentLoaded()

{

this.SetValue("WhereCondition", "NewsTitle LIKE '%News%'");

base.OnContentLoaded();

}

</script>

 

 

InfoBox_Note

 

Calling the original method from the inherited class

 

If you override some method of the web part, please be sure to always also call the original method (using base.Method in C# or MyBase.Method in VB). If you omit this, the web part may not work properly.