Ok. Really trying to get back into blogging. Instead of always doing something complex, try to do something simple.
In this case. I want to discuss an issue I was working with R.A.D. ComboBox this week.
I'm continuing work on the YakShaver. Working on a portion of the UI in which case I use logical groups to divide out customers from projects. These groups contain a very small number of elements, anywhere from 1-5. Picture a tree structure such that
- Project 1
- Project 2
- Project 3
- And So on...
It also allows us to be more creative by using the to add all sorts of pizazz to our combo box improving the user experience; which was exactly what I intended to do. In my case, I was turning the drop down into a checkbox that I would validate later.
Hence came my problem.
The item template is invoked for every RadItem that exists, regardless of if its a separator or not. Having a simple table to handle our structure doesn't work. In my case what happened was every combo item would render with a checkbox, including my separator items. I wanted to add some conditional logic that says "If this is a separator item, then display this, else this."
Typically I would not put any sort of server side code in the UI, but this seemed to be one of the instances that it made the most sense. I wanted easy access to the the item being bound (the RadComboBoxItem) to tell if it was a separator. With no good way from what I could find with a declarative syntax, I thought about how I could do it conditionally inside item template.
Problem, how do I access the item? Answer: parameterContainer.
parameterContainer is of type System.Web.UI.Control that represents the item being injected into the template. In our case, it was the RadComboBoxItem. With a simple cast, I can access all the properties and methods in the aspx and get type safely (plus intellisense!).
I can use the standard <% %> syntax and therefore not have to have a dependency of HTML within the code behind. Here is an example.
1: <telerik:RadComboBox runat="server" ID="rcbComponentDeploySites" OnLoad="cmb_Load">
3: <% if (!(parameterContainer as RadComboBoxItem).IsSeparator)
6: <table border="0">
9: <asp:CheckBox runat="server" ID="chkDeployToSite" />
12: <%# DataBinder.Eval(Container, "Attributes['DisplayName']") %>
13: (<%# DataBinder.Eval(Container, "Text") %>)
22: <table border="0">
24: <td style="border-bottom: 1px dotted black;">
25: <%# DataBinder.Eval(Container, "Attributes['DisplayName']") %>
26: (<%# DataBinder.Eval(Container, "Text") %>)
35: <telerik:RadComboBoxItem runat="server" IsSeparator="true" Text="Customer 1 Sites" DisplayName="Customer 1 Sites" />
36: <telerik:RadComboBoxItem runat="server" Text="Customer Site 1" />
37: <telerik:RadComboBoxItem runat="server" Text="Customer Site 2" />
38: <telerik:RadComboBoxItem runat="server" IsSeparator="true" Text="Customer 2 Sites" DisplayName="Customer 2 Sites" />
39: <telerik:RadComboBoxItem runat="server" Text="Customer Site 1" />
40: <telerik:RadComboBoxItem runat="server" Text="Customer Site 2" />
This should be true of anything that implements ITemplate. It gives a great deal of flexibility to the UI and still allows for type safe programming.