Adding custom code to web parts (obsolete)

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

 

 

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.

 

You can find more details in the Modifying web part behavior topic.

 

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" />

 

 

 

Limitations when you use 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 medium trust environment. If this is an issue for you, you can use an alternative solution described in the Modifying web part behavior topic.

 

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>

 

 

 

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>

 

 

 

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.

 

Page url: http://devnet.kentico.com/docs/5_5r2/devguide/index.html?adding_custom_code_to_web_parts.htm