11

I created a simple custom control that only inherits from the Literal control, and doesn't have any extensions yet, code is empty.

Namespace: CustomControls

Class name: Literal : System.Web.UI.WebControls.Literal

Next thing I do is registering this control in the aspx page as following:

<%@ Register TagPrefix="web" Namespace="CustomControls" %>

(I read in few tutorials that this is one of the ways to register it, besides web.config etc.)

After all, no intellisence for me, and worse- I get a parse error 'unknown server tag: web' when I try to run the page with the control in it.

I used 'create new project' and not new website, in case this info is needed.

What could be my problem?

Thanks in advance.

geevee
  • 5,411
  • 5
  • 30
  • 48
  • What exactly do you mean by "create new project"? File -> New -> Project -> ... (at least in VS 2005) And then, what did you coose? That might be important (i.e. did you choose Other Languages -> Visual C# -> Web -> whatever?). And you do need an assembly at some point; the question is if you have to explicitly name it (and I think you have to if you are not using a "Website" project as you do not have an App_Code folder). – scherand Jun 09 '10 at 06:56
  • Where does your custom control class exist? Is it a class file in the website project or is it in its own project? – Scott Mitchell Jan 04 '11 at 21:31
  • So you have two projects one for the 'CustomControls' and another for the web project? – Craig Jan 04 '11 at 21:44
  • @scherand : I think he means he is doing this as a Web Application Project and not as a Website Project. The two behave a little differently. – Maximillian Jan 05 '11 at 15:55
  • the problem occurs in WebApplicationProject in VS2008 – Mahes Jan 05 '11 at 19:22

5 Answers5

28

Here is how I did it, step by step starting from nothing. This first method uses a second project/assembly. For the App_code version scroll down.

Web Application Project Method

  1. Create a new ASP.Net Web Application. Take note of the name, mine is called WebApplication2. If you already have an existing web application, which is likely, double click the properties section of your project and check the "Assembly Name" property, make note of it.
  2. Create a new Class in the web applcation named Literal.cs
  3. Put in code similar to the following for the class defenition:

    namespace CustomControls
    {
        public class Literal : System.Web.UI.WebControls.Literal
        {
        }
    }
    
  4. Add the following register tag to the top of your aspx page

    <%@ Register assembly="WebApplication2" namespace="CustomControls" tagprefix="web" %>

If your assembly name was different then change it here. I noticed that when I did this in VB.Net the namespace was WebApplication1.CustomControls instead of just CustomControls like it was in C#, kind of odd.

  1. Add the new control to your page:

    <web:Literal ID="Literal1" runat="server" Text="test" />

Seperate Project Method

  1. Create a new Empty Website (ASP.Net).
  2. Add a new ASP.Net Server Control library named CustomControls to the solution.
  3. Add a new Class to the new project called Literal (I'm using C# so my file is named Literal.cs). Below is my super basic code, that I believe should match the code described in the question.

    namespace CustomControls
    {
        public class Literal : System.Web.UI.WebControls.Literal
        {
        }
    }
    
  4. Add a reference to the CustomControls project to your website.

  5. Add the assembly registration to the top of your aspx page:

    <%@ Register assembly="CustomControls" namespace="CustomControls" tagprefix="web" %>

  6. Add a new instance of the control to your page:

    <web:Literal ID="Literal1" runat="server" Text="test" />

In App_Code Method

  1. Create a new Empty Website (ASP.Net).
  2. Add a new Class to the App_Code folder Literal2 (I'm using C# so my file is named Literal2.cs). Below is my super basic code, that I believe should match the code described in the question. I called it 2 so that you can use this in conjunction with the method described above without getting compile errors

    namespace CustomControls
    {
        public class Literal2 : System.Web.UI.WebControls.Literal
        {
        }
    }
    
  3. Register the assembly/namespace for app_code in your aspx page by adding the following line to the top

    <%@ Register Namespace="CustomControls" Assembly="__code" tagprefix="web" %>

  4. Add an instance of the new control to your page:

    <web:Literal2 ID="literal2" runat="server" Text="test2" />

I tested this using visual studio and it all worked fine for me.

Peter
  • 9,643
  • 6
  • 61
  • 108
  • Thanks for the detailed information, this works perfectly in web site project. the problem is with WebApplicationProject in VS2008. – Mahes Jan 05 '11 at 19:27
  • 1
    @Mahes - I've updated my answer to include Web Application Directions (they are at the top). – Peter Jan 06 '11 at 14:45
  • @Mahes - Please let me know if the code I posted does not work for you. – Peter Jan 10 '11 at 15:04
2

You can also register your control in web.config if you would like to use it on other pages

<pages>
        <controls>
          <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
          <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
          <add tagPrefix="web" namespace="MyProject.CustomControls" assembly="MyProject" />
        </controls>
      </pages>

For web site this will work

<add tagPrefix ="web" namespace="ASP.App_Code.CustomControls" />
orjan
  • 1,482
  • 1
  • 19
  • 35
  • 1
    Thanks for the response, The thing is- I don't have any assembly file yet, and as I understood, the control can be registered by tagprefix and namespace only, even without assembly. am I right ? – geevee Jun 09 '10 at 06:00
  • 1
    If you have created a web application project you will have an assembly, see: http://dl.dropbox.com/u/6561262/project-settings.png – orjan Jun 09 '10 at 06:13
2

The problem lies in your <%@ Register TagPrefix="web" Namespace="CustomControls" %> tag: it misses the assembly name!

It must become <%@ Register TagPrefix="web" Namespace="CustomControls" Assembly="YourAssemblyName" %>. If you have difficulties finding the assembly name for your class here are a few hints:

  1. If you created the class inside the web application's Visual Studio project, or in a separate DLL (shared library) project, then right-click the project and go to Properties to get the assembly name
  2. If you created the .cs file in App_Code directory, ASP.App_Code should work

Otherwise please give us some information about project layout!

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305
  • thanks. even specifying assembly name doesnt sovles the problem with WebApplicationProject in VS2008 – Mahes Jan 05 '11 at 19:23
1

The easiest thing to do is to drag-and-drop the Literal.ascx on to the page (if the page is in the same solution) .If the page is another solution, you could add the control to VS toolbox and drag-and-drop the control on the page.

Vijay Sirigiri
  • 4,653
  • 29
  • 31
0

You forgot the src attribute.

<%@ Register TagPrefix="web" Namespace="CustomControls" Src="PathToYourCustomControl" %>