Executing Other Functions
Execute other functions in User Control functions
You can execute other CMS Functions in User Control functions.
Using Function Markup in ASCX files
You can use the function's markup to execute a CMS Function (similar to how you do in XSLT functions or XHTML templates).
.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ExecuteFunctionMarkup.ascx.cs" Inherits="C1Function" %> <f:function name="Composite.Navigation.Distributed" runat="server"> <f:param name="Level" value="1" /> <f:param name="ShowParent" value="False" /> <f:param name="ShowChildPages" value="True" /> <f:param name="Expand" value="False" /> <f:param name="NavigationId" value="NavigationSideBar" /> </f:function>
.ascx.cs
using System; using Composite.Functions; public partial class C1Function : Composite.AspNet.UserControlFunction { public override string FunctionDescription { get { return "A demo function that executes the Distributed Menu function."; } } protected void Page_Load(object sender, EventArgs e) { } }
You can quickly insert the function markup via the menu: Insert | Function Markup.
When you manually insert the function markup, make sure to specify runat="server"
.
Executing functions in ASCX.CS files
You can execute standard .NET code in .ascx.cs files.
.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="HelloWorldWithQueryString.ascx.cs" Inherits="C1Function" %> Hello <%= this.Name %>!
.ascx.cs
using System; using Composite.Functions; public partial class C1Function : Composite.AspNet.UserControlFunction { public override string FunctionDescription { get { return "A demo function that outputs a hello message read from the query string parameter."; } } [FunctionParameter(DefaultValue = "World")] public string Name { get; set; } protected void Page_Load(object sender, EventArgs e) { string Param1 = Request.QueryString["Param1"]; // fall back to the default value if no query parameter is specified if(!String.IsNullOrEmpty(Param1)) Name = Param1; } }
If you need to execute some CMS Functions, make use of the FunctionFacade
's methods to get the function by name (GetFunction
) and execute it (Execute
).
.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ExecuteWithFunctionFacade.ascx.cs" Inherits="C1Function" %> <%= this.distributedMenu %>
.ascx.cs
using System; using Composite.Functions; using Composite.Core.Xml; using System.Collections.Generic; using System.Xml.Linq; public partial class C1Function : Composite.AspNet.UserControlFunction { public override string FunctionDescription { get { return "A demo function that outputs executes the Distributed Menu function."; } } public XhtmlDocument distributedMenu { get; set; } protected void Page_Load(object sender, EventArgs e) { distributedMenu = FunctionFacade.Execute<XhtmlDocument>( FunctionFacade.GetFunction("Composite.Navigation.Distributed"), new Dictionary<string, object>() { { "Level", 1 }, { "ShowParent", false }, { "ShowChildPages", true }, { "Expand", false }, {"NavigationId", "NavigationSideBar"} }); // removing the class attribute from the level menu's elements foreach (var item in distributedMenu.Descendants()) { item.SetAttributeValue("class", null); } } }