# Wednesday, January 06, 2010
« Tip of the Day: Dynamically sorting a Li... | Main | Tip of the Day: Manipulating ASP.Net val... »

The latest project I am working on is a web site project, which differs significantly from a web application project. If you need a little background on the difference, here's some links:

http://www.codersbarn.com/post/2008/06/ASPNET-Web-Site-versus-Web-Application-Project.aspx

http://reddnet.net/code/aspnet-web-site-vs-web-application/

We wanted to create some base controls, such as for the GridView so that common functionality could be consistant across the web site. It's simple to create a user control to do this (e.g. an .ascx file), but in this case we wanted to do it as a class.

In the web application, that's no problem because we know what the assembly name is. In a web site template, we don't, so how do we register the control for use? It took a few minutes of digging around and trying a few things, but I finally got it, and I decided to share it for the next person looking.

Let's say I define my base GridView class like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace BaseControls
{
/// <summary>
/// Summary description for BaseGrid
/// </summary>
public class BaseGrid : GridView
{
   public BaseGrid()
   {
      //
      // TODO: Add constructor logic here
      //
   }

   public string SomeMethod(string someString)
   {
      return someString;
   }

}
}

Note the namespace. By default the web project may not have it, so you'll need to create one.

In order to reference it on the page, you'll actually leave the assembly blank. Your register tag will look something like this:

<%@ Register Namespace="BaseControls" TagPrefix="wf" %>

Now, to use the control, all you need to do is put this on the aspx page.

<wf:BaseGrid ID="grdReport" runat="server" Width="95%"…. (use it like a normal GridView)

In code, you can then use the properties/methods like this:

grdReport.SomeMethod("test");


Comments are closed.