My Development Blog

My Development experiences

Archive for November, 2013

Kentico – Inline Data Binding

Posted by ipwright83 on November 12, 2013

My first Kentico page resulted in a bit of a mess. It involved lots of WebPartZones, and then each zone had a WebPart in that would then be bound to the CMSContext.CurrentDocument.SomeField. This allowed me to bind the name, description, image etc…

Unfortunately it quickly looks a mess in CMSDesk, is quite time consuming and it just a bit of a pain working in CMSDesk for all this content. I asked around and my team pointed out that I could do in-line data binding like so:

1
2
3
4
<div class="imageInfo">
   <h1><%= CMSContext.CurrentDocument.GetStringValue("Title", "{Title}") %></h1>
   <p><%= CMSContext.CurrentDocument.GetStringValue("Description", String.Empty) %></p>
</div>

The problem is at this point things like the Designer view in CMSDesk will frequently complain with the error:

The Controls collection cannot be modified because the control contains code blocks

This is rather annoying, and consequently developers have either used the approach of adding lots of WebParts (the norm) or sometimes assigning values in the code behind. What I find easier however is to change the syntax slightly to use data binding:

1
2
3
4
<div class="imageInfo">
   <h1><%# CMSContext.CurrentDocument.GetStringValue("Title", "{Title}") %></h1>
   <p><%# CMSContext.CurrentDocument.GetStringValue("Description", String.Empty) %></p>
</div>

Now this won’t work either directly, because your page probably hasn’t been data bound. The easy way to fix this is to ensure you call DataBind(). In my case I wanted to put this in the masterpage, but the masterpage from Kentico didn’t feature a code behind file that I could easily modify so I simply changed the master page inherits it like so:

1
<%@ Control Language="C#" Inherits="MasterPageLayout" %>

Now in page load I can call DataBind:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.PortalControls;

namespace NetC.BurtonRoofing
{
    /// <summary>
    /// Code behind for the Master Page
    /// <remarks>Allows the use of markup in the ascx such as </remarks>
    /// </summary>
    public class MasterPageLayout : CMSAbstractLayout
    {
        /// <summary>
        /// Raises the <see cref="E:Load" /> event.
        /// </summary>
        /// <remarks>Calls the DataBind method for the page</remarks>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Page.DataBind();
        }
    }
}

This makes data binding nice and simple, keeping your binding in your designer code without needing to mess around with WebPartZones.

Advertisements

Posted in Uncategorized | 2 Comments »

WCF Services – Dispose can cause you problems

Posted by ipwright83 on November 7, 2013

Normally when you’re working with something that implements IDisposable you should always call Dispose() on it. Unfortunately there are a whole bunch of classes that implement IDisposable implicitly. What that means is that you don’t see Dispose in intelli-sense, which makes it quite difficult to check. The general rule is if it’s going to deal with Stream/Connections/GDI/Databases there will probably be a dispose in there somewhere. The simplest way to do this is to wrap the code in a using statement to ensure that Dispose is called even if there is an error.

Once such example of this is the WCFClient. You would normally expect to write something like the following

using(PaymentPortTypeClient client = new PaymentPortTypeClient())
{
    // Do Stuff
}

Unfortunately however for WCF the base class System.ServiceBase.ClientBase can throw an Exception in Dispose if the connection failed to open for any reason. This is a bit of a pain, so instead we need to conditionally dispose depending on the connection state.

A little helper for achieving this:

 ///
/// Use the service safely such that it will correctly dispose itself upon success or failure.
///
/// The delegate to execute safely 
public static void Use<T>(UseServiceDelegate codeBlock) 
{ 
   // Ensure that the factory has been initialized 
   IClientChannel proxy = (IClientChannel)channelFactory.CreateChannel(); 
   bool success = false; 
   try 
   { 
       codeBlock((T)proxy); 
       proxy.Close(); 
       success = true; 
   } 
   finally 
   { 
       if (!success) 
       { 
           proxy.Abort(); 
       } 
   } 
}

At this point assuming you’ve placed the method in a class called Service you can use the following code to safely call the same code that Dispose would on your connection:

Service.Use<PaymentPortType>(proxy =>
{ 
   // Do Stuff
}

Posted in C# | Tagged: , | 2 Comments »