Thursday, August 18, 2005

LinkList Class (C#, ASP.NET)

This class can be used to generate a list of links that could be used for breadcrumbs or menu navigation. Add items to the list as follows (1st parameter is text, 2nd is the page url and 3rd is the tooltip, which is optional):

LinkList links = new LinkList();
links.Add(new LinkList.Link("Home", ResolveUrl("home.aspx"), "Home Page"));
links.Add(new LinkList.Link("Page 1", ResolveUrl("page1.aspx")));
links.Add(new LinkList.Link("Page 2", ResolveUrl("page2.aspx")));
links.Add(new LinkList.Link("Page 3", ResolveUrl("page3.aspx")));
// need to set CssClass for 'Inline' and 'Block' types
links.CssClass = "myclass";

There are four types of list types. Shown in the order they are added in. Also, if the current page you are on is the same as one in the list, the text is displayed instead of the link.

One is 'Inline', which surrounds the links with a span tag (only if you set the CssClass to a non-blank string) and separates the links with whatever you define (the default is the > symbol):

// use inline type (surround with span tags)
links.ListType = LinkList.ListTypes.Inline;
this.Controls.Add(new LiteralControl("<p>Inline ListType</p>"));
this.Controls.Add(new LiteralControl(links.ToString()));

Inline ListType

Home > Page 1 > Page 2 > Page 3

Another is 'Block' (works in same way as 'Inline', but uses a div tag):

// use block type (surround with div tags)
links.ListType = LinkList.ListTypes.Block;
// change separator
links.Separator = " | ";
this.Controls.Add(new LiteralControl("<p>Block ListType</p>"));
this.Controls.Add(new LiteralControl(links.ToString()));

Block ListType

Another is 'OrderedList':

// use ordered list type (surround with ol, li tags)
links.ListType = LinkList.ListTypes.OrderedList;
this.Controls.Add(new LiteralControl("<p>OrderedList ListType</p>"));
this.Controls.Add(new LiteralControl(links.ToString()));

OrderedList ListType

  1. Home
  2. Page 1
  3. Page 2
  4. Page 3

Finally 'UnorderedList':

// use unordered list type (surround with ul, li tags)
links.ListType = LinkList.ListTypes.UnorderedList;
this.Controls.Add(new LiteralControl("<p>UnorderedList ListType</p>"));
this.Controls.Add(new LiteralControl(links.ToString()));

UnorderedList ListType

The code that actually does the work is as follows:

using System;
using System.Collections;
using System.Text;
using System.Web;

namespace WebDeveloperBlog
{  
 public class LinkList : CollectionBase
 {
  public class Link
  {
   private string url;
   public string Url
   {
    get
    {
     return url;
    }
    set
    {
     url = value;
    }
   }
   private string text;
   public string Text
   {
    get
    {
     return text;
    }
    set
    {
     text = value;
    }
   }
   private string tooltip;
   public string Tooltip
   {
    get
    {
     return tooltip;
    }
    set
    {
     tooltip = value;
    }
   }
   public Link(string text, string url)
   {
    this.Text = text;
    this.Url = url;
   }
   public Link(string text, string url, string tooltip)
   {
    this.Text = text;
    this.Url = url;
    this.Tooltip = tooltip;
   }
  }
  
  public string cssClass = string.Empty;
  public string CssClass
  {
   get
   {
    return (cssClass);
   }
   set
   {
    this.cssClass = value;
   }
  }
  
  public enum ListTypes
  {
   Inline, Block, OrderedList, UnorderedList
  }
  
  public ListTypes listType = ListTypes.Inline;
  public ListTypes ListType
  {
   get
   {
    return (listType);
   }
   set
   {
    this.listType = value;
   }
  }
  
  public string separator = " > ";
  public string Separator
  {
   get
   {
    return (separator);
   }
   set
   {
    this.separator = value;
   }
  }
  
  public override string ToString()
  {
   StringBuilder sb = new StringBuilder();
   if (ListType == ListTypes.OrderedList)
   {
    sb.Append("<ol");
    if (CssClass != string.Empty)
    {
     sb.Append(" class=\"" + CssClass + "\"");
    }
    sb.Append(">");
   }
   if (ListType == ListTypes.UnorderedList)
   {
    sb.Append("<ul");
    if (CssClass != string.Empty)
    {
     sb.Append(" class=\"" + CssClass + "\"");
    }
    sb.Append(">");
   }
   if (CssClass != string.Empty && ListType == ListTypes.Inline)
   {
    sb.Append("<span class=\"" + CssClass + "\">");
   }
   if (CssClass != string.Empty && ListType == ListTypes.Block)
   {
    sb.Append("<div class=\"" + CssClass + "\">");
   }
   foreach(Link item in this)
   {
    if(this.IndexOf(item)!=0 && (ListType == ListTypes.Inline || ListType == ListTypes.Block))
    {
     sb.Append(HttpContext.Current.Server.HtmlEncode(Separator));
    }
    if (ListType == ListTypes.OrderedList || ListType == ListTypes.UnorderedList)
    {
     sb.Append("<li>");
    }
    if(item.Url != HttpContext.Current.Request.Url.AbsolutePath)
    {
     sb.Append("<a href=\"" + item.Url + "\"");
     if (item.Tooltip != null)
     {
      sb.Append(" title=\"" + item.Tooltip + "\"");
     }
     sb.Append(">" + item.Text + "</a>");
    }
    else
    {
     sb.Append(item.Text);
    }
    if (ListType == ListTypes.OrderedList || ListType == ListTypes.UnorderedList)
    {
     sb.Append("</li>");
    }
   }
   if (CssClass != string.Empty && ListType == ListTypes.Inline)
   {
    sb.Append("</span>");
   }
   if (CssClass != string.Empty && ListType == ListTypes.Block)
   {
    sb.Append("</div>");
   }
   if (ListType == ListTypes.OrderedList)
   {
    sb.Append("</ol>");
   }
   if (ListType == ListTypes.UnorderedList)
   {
    sb.Append("</ul>");
   }
   return sb.ToString();
  }
  
  public Link this[int index]
  {
   get
   {
    return ((Link)(this.List[index]));
   }
   set
   {
    this.List[index] = value;
   }
  }
  
  public int Add(Link value)
  {
   return this.List.Add(value);
  }
  
  public void Insert(int index, Link value)
  {
   this.List.Insert(index, value);
  }
  
  public int IndexOf(Link value)
  {
   return this.List.IndexOf(value);
  }
  
  public bool Contains(Link value)
  {
   return this.List.Contains(value);
  }
  
  public void Remove(Link value)
  {
   this.List.Remove(value);
  }
  
  public void CopyTo(Link[] array, int index)
  {
   this.List.CopyTo(array, index);
  }
 }
}

Tags: , ,

No comments: