Adding Custom Cascading Stylesheets
This topic describes how to add custom Cascading StyleSheets (CSS) to an existing custom control .
Three things are needed:
- An embedded style sheet file
- Enabling the embedded Style Sheet file as a WebResource
Setting the ClientCSS Attribute value to point to the embedded .css resource file
- This project on Github contains the sample project described in this topic. You can view the project on Github instead of writing out the code, if you prefer:
K2Documentation.Samples.Extensions.SmartForms.CustomControl -
JavaScript and CSS Support for custom scripts and custom controls:
- ECMAScript version: ES2025
- CSS version: CSS snapshot 2024
Adding a custom Cascading Stylesheet
- Add a Stylesheet.css file to the project as an embedded file
- Edit the new css file and use the SFC class object referencing schema to define a span definition for the displayed text, as demonstrated in the span node displayed below:
- In the custom control's server-side code file:
- Add an assembly WebResource attribute that points to the embedded CSS embedded resource file
- Set the ClientCSS attribute to point to the embedded resource file,
See the code snippet below for an example:CopySet the ClientCSS attribute
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// SourceCode namespaces used in the control
using SourceCode.Forms.Controls.Web.SDK;
using SourceCode.Forms.Controls.Web.SDK.Attributes;
// Point to the style resource file
[assembly: WebResource("CustomControl.Styles.css", "text/css", PerformSubstitution = true)]
namespace CustomControl
{
//Link to the definition file
[ControlTypeDefinition("CustomControl.Definition.xml")]
[ClientCss("CustomControl.Styles.css")]
class StyleControl : BaseControl
{
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
writer.Write("Control Styled");
}
}
}
- Build and deploy the custom control assembly
- Restart the IIS service and clear your browser cache
- In the Designer refresh your View. You should see the result of the cascading style sheet as the background is changed to green