JavaScript and CSS controls for ASP.NET

by Matt 21. February 2009 08:32

It’s often troublesome to link to JavaScript and CSS files in ASP.NET pages when the desired files can’t easily be accessed from the current path. For example, when setting up SyntaxHighlighter on this site, it seemed like good practice to not include the JS and CSS files in the BlogEngine.NET theme directory, but in the root of the application. After all, any theme should be able to access these files. ASP.NET provides the root operator for just this purpose. Unfortunately, it doesn't work with client-side markup, and there are no server-side controls for styles or scripts. One solution is to resolve the path manually:

<script type="text/javascript" src='<%= ResolveUrl("~/path/to/script.js") %>'></script>

...but that's not particularly clean. A better way is to create a control specifically for this purpose, so one can do the following:

<controls:JavaScript Src="~/path/to/script.js" runat="server" />
<controls:Css Href="~/path/to/style.css" runat="server" />

The source for these controls is really simple. The JS and CSS controls contain the Src and Href properties to be similar to their respective markup. The JS control:

public class JavaScript : Control
{
    public String Src { get; set; }

    protected override void Render(HtmlTextWriter writer)
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");
        writer.AddAttribute(HtmlTextWriterAttribute.Src, ResolveUrl(Src));
        writer.RenderBeginTag(HtmlTextWriterTag.Script);
        writer.RenderEndTag();
    }
}

And the CSS control:

public class Css : Control
{
    public String Href { get; set; }

    protected override void Render(HtmlTextWriter writer)
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
        writer.AddAttribute(HtmlTextWriterAttribute.Href, ResolveUrl(Href));
        writer.RenderBeginTag(HtmlTextWriterTag.Link);
        writer.RenderEndTag();
    }
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

Tips and Tricks

An indexed foreach method for C#

by Matt 18. February 2009 23:37

Foreach loops are rather convenient. In most cases, it's cleaner to write:

var list = new List { 5, 2, 3, 10, 9 };
foreach(var item in list)
{
    doSomething(item);
}

Rather than:

var list = new List<int> { 5, 2, 3, 10, 9 };
for(int i = 0; i < list.Count; i++)
{
    doSomething(list[i]);
}

...which only works with collections that implement ICollection. Say you wanted to perform some action that required a reference to the item and an index or counter associated with the item. You'd have a separate counter variable, which is slightly messy:

var list = new List<int> { 5, 2, 3, 10, 9 };
int count = 0;
foreach(var item in list)
{
    Console.WriteLine(String.Format("Item {0} has index of {1}.", item, count));
    count++;
}

Using extension methods in C# 3.0, we can extend IEnumerable to have an "indexed" foreach that supplies us with an index. The following extension method lets us do just that.

public static void ForEach<T>(this IEnumerable<t> self, Action<T, int> action)
{
    int index = 0;
    foreach (var item in self)
    {
        action(item, index);
        index++;
    }
}

Then, our foreach can be rewritten:

var list = new List<int> { 5, 2, 3, 10, 9 };
list.ForEach(delegate(int item, int index)
{
    Console.WriteLine(String.Format("Item {0} has index of {1}.", item, index));
});

It can be made more compact using lambda expressions:

var list = new List<int> { 5, 2, 3, 10, 9 };
list.ForEach((item, index) =>
{
    Console.WriteLine(String.Format("Item {0} has index of {1}.", item, index));
});

We can also define an extension method for the non-generic IEnumerable interface:

public static void ForEach(this IEnumerable self, Action<Object, int> action)
{
    int index = 0;
    foreach (var item in self)
    {
        action(item, index);
        index++;
    }
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

Tips and Tricks