Adding custom code to web parts (obsolete)

 

 

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 chapter Modifying web part behavior.

 

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 web site and you will not be able to use the web site in medium trust environment. If this is an issue for you, you can use an alternative solution described in chapter Modifying web part behavior.

 

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:

 

clip0583

 

The following code displays 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 use only 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 call also the original method (using base.Method in C# or MyBase.Method in VB). If you omit this, the web part may not work properly.