Background & The Problem

Last time we discussed how to integrate a custom control into an XSL transformation. The key point to note about the previously discussed approach is that all the markup is output by the overridden Render method. This can easily get cumbersome and ineffective for complex server side controls. The ASP.net stack offers us a solution to this common problem with Control definitions. This solution is a UserControl which adds a special ASCX file containing declarative markup which makes the development task much easier, simpler & cleaner.

In this post, we will show how to incorporate a server side UserControl with markup in it’s ASCX file to render within an XSLT instead of a simple Control definition.

How to do this

In order to render a UserControl ASCX within an XSLT, we need to follow a slightly different method that the one discussed in the previous post. Here are the steps-

  1. First thing we need to do is to define our user control. Here is a sample ASCX definition. Notice that this control renders a variable called ‘Input’ which we will also define in the code-behind class in the next step-

  2. Next, we define the code-behind that our ASCX inherits from. Notice that we are declaring a public string property called ‘Input’ whose value is being directly rendered within the ASCX as an example. This variable is used to accept contextual information about the XML node being processed by the XSL transform at the time of evaluating this control during the runtime-

  3. Next, we need to globally register our user control so that our XSLT can find it. This is done because we don’t have the ability to register custom user controls from within an XSLT webpart. The following registration happens in the web.config in this node: <configuration>…<system.web>…<pages>…<controls>. Notice that we assign a tagName for this user control. This tag name will be used when we reference the user control in our XSLT-

  4. Now that SharePoint globally knows about this user control, we need to modify our xsl declaration to assign a tag prefix for this control so that we can reference it wherever we want to use it. In this case, our tag prefix is ‘PSControl’-

  5. Now that everything has been setup, we can simply reference our custom user control using the tag prefix defined in step 4 (PSControl) and the tag name defined in step 3 (PSC). Combined this tag declaration allows us to render our custom user control ASCX which was written declaratively to save development time associated with defining a custom server side control for our XSLT. Notice that we are using the Input property defined in the UserControl and passing it contextual value about the current node  being processed in this case-

That’s it! You can now write declarative markup in a UserControl ASCX file and have that markup process XML nodes using a code-behind, all within the context of an XSL transform.

Tagged with:
 

Leave a Reply

Your email address will not be published.

Set your Twitter account name in your settings to use the TwitterBar Section.