Month: February 2009

Generating Tree-like UI structures

In many of my projects I have had to generate non-binary trees using information from the database and then display them in a “tree-like” structure as shown below.

blog

My algorithm is very simple and it makes use of Dan Vanderboom’s excellent Generic Tree Collection but frankly I’m not satisfied with certain aspects of my algorithm. The main issue – the data can never be retrieved in the same order as shown above from the database.

Code

Now that we have a collection ready, we can generate the tree structure

Code

Finally, I add the items to my drop-down control by calling 

GenerateDropDownList(rootCategory, 0);

Let’s have a look at the function

blog4

I know I could have used LINQ but many of my projects are running in .NET 2.0 so I had to go old-school! I’m looking forward to your valuable comments as to how YOU would do this and why your solution is more efficient than mine.

Happy Programming!

kick it on DotNetKicks.com

Share

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

I’m pretty much sure many of you have encountered this error if you have been using Code blocks inside your ASP.NET master pages for example,

   1:  <img src='<%= Page.ResolveUrl("~/image/top-mnu-agents.jpg") %>' alt="Agents" name="agents"
   2:                                                  width="109" height="42" border="0" id="agents" /></a>

Let’s say you want to use code blocks inside your JavaScript code, for example

   1:  function PreloadImages()
   2:  {
   3:         /*MM_preloadImages('<%= Page.ResolveUrl("~/image/top-mnu-property.jpg") %>',
   4:                           '<%= Page.ResolveUrl("~/image/top-mnu-agents.jpg") %>',
   5:                           '<%= Page.ResolveUrl("~/image/top-mnu-company.jpg") %>',
   6:                           '<%= Page.ResolveUrl("~/image/top-mnu-contact.jpg") %>');*/
   7:  }

Is this going to work? Make a guess! No, its not! Bump! Even though the code block is inside a comment block! How’s that! If you are curious why, ask the guys at Microsoft 😉

The workaround to this problem is Data Biding Expressions. Milan Negovan has discussed this in greater detail in this article.

Happy Programming!

Share