20 Essential Web Utilities

April 23, 2007 at 10:17 pm (Utilities)

DotNetGuts has a pretty good article listing 20 essential web utilties that should be used by web developers and web designers.



Permalink Leave a Comment

SQL Snippet for Paging

April 18, 2007 at 11:49 am (SQL)

Microsoft SQL 2005 finally introduced better support for doing paging at the database level. I’m a little disappointed that MS is not pushing to use this as the default method for paging, instead of relying on ASP.NET controls.

1 USE AdventureWorks 2 GO 3 4 SELECT * FROM ( 5 SELECT 6 ROW_NUMBER() OVER (ORDER BY SellStartDate ASC) AS rownumber,* 7 FROM Production.Product 8 ) AS Product 9 WHERE rownumber between @FromRow AND @ToRow

Permalink Leave a Comment

Using Windows Live Writer

April 18, 2007 at 1:56 am (blog)

After recently setting up my blog on WordPress, I decided to use Windows Live Writer to post to my blog. I am quite impressed at how easily and effortlessly it was to connect and post to the blog.

It’s not only the Live Writer itself but all the plugins that really make it easy to format (espcially code snippets) posts.

Permalink Leave a Comment

URL Rewriting with ASP.NET and Handling Images and CSS

April 17, 2007 at 12:38 pm (ASP.NET)

As usually, ScottGu has an excellent post about URL rewriting with ASP.NET. No need for me to repeat what he already said 🙂

However at the end of his post, Scott has a section for dealing with CSS and Image references correctly, which has been causing problems for some including myself. The problem is with the use of the tilde (~) notation for ASP.NET controls and for the CSS you must use the absolute path meaning “/style.css” instead of just “styles.css”.

Handling CSS

Of course you can simply put an absolute path and your done. The solution I’m proposing here is an alternative and simulates the ~ notation. This solution was inspired by what Scott did with the forms control adapter.

Looking at typical page (including master pages) we have the following:

1 <head runat="server"> 2 <title>My Page</title> 3 <link href="Stylesheet.css" rel="stylesheet" type="text/css" /> 4 </head>

So that got me thinking that since the head is now a server it must also follow the ASP.NET rendering methods. So based on Scott’s sample you can add the following to the Form.browser file:

1 <browsers> 2 <browser refID="Default"> 3 <controlAdapters> 4 <adapter controlType="System.Web.UI.HtmlControls.HtmlForm" 5 adapterType="FormRewriterControlAdapter" /> 6 <adapter controlType="System.Web.UI.HtmlControls.HtmlLink" 7 adapterType="LinkRewriterControlAdapter" /> 8 </controlAdapters> 9 </browser> 10 </browsers>

Essentially we are going to override the rendering for the HtmlLink control. The C# code to do that (LinkRewriterControlAdapter) is:

1 public class LinkRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter 2 { 3 protected override void Render(HtmlTextWriter writer) 4 { 5 base.Render(new RewriteLinkHtmlTextWriter(writer)); 6 } 7 8 } 9 10 public class RewriteLinkHtmlTextWriter : HtmlTextWriter 11 { 12 #region Constructors 13 14 public RewriteLinkHtmlTextWriter(HtmlTextWriter writer) : base(writer) 15 { 16 this.InnerWriter = writer.InnerWriter; 17 } 18 19 public RewriteLinkHtmlTextWriter(System.IO.TextWriter writer) : base(writer) 20 { 21 this.InnerWriter = writer; 22 } 23 24 #endregion 25 26 public override void WriteAttribute(string name, string value, bool fEncode) 27 { 28 if (name == "href") 29 { 30 HttpContext Context = HttpContext.Current; 31 if (Context.Items["ActionAlreadyWritten"] == null) 32 { 33 string sStylesheetName = value; 34 int iSlashPos = value.LastIndexOf('/'); 35 if (iSlashPos >= 0) sStylesheetName = value.Substring(iSlashPos); 36 37 value = GetBaseURL() + "/" + sStylesheetName; 38 39 Context.Items["ActionAlreadyWritten"] = true; 40 } 41 } 42 base.WriteAttribute(name, value, fEncode); 43 } 44 }

This will override the rendering of the href attribute value for all <link> tags. The value coming in has already been resolved by ASP.NET, that is why there is some parsing for slash (/).

Handling Images

The tilde notation (~) works great if you are only doing one level of URL rewriting. If you want to do URL rewriting with mulitple levels ex: http://mysite.com/products/category/subcategory/subsubcategory/etc.., then (~) notation will no longer work since ASP.NET will resolve the URL relative to real ASPX page. For this case I find it simplest to use HTML elements and specify the href with inline code. Ex:


1 <a href="<% =GetBaseURL() %>/">Home</a>

Hope this helps. Please feel free to comment (go easy this is my first post :))

Permalink 23 Comments