views:

207

answers:

3

I am trying to create my first ASP.net server control derived from a Panel. For some reason, I am unable to get the .aspx page to recognize my server tag even though the application recognizes the class and namespace of my control.

Here are the steps I've used:

1) I created a class CollapsablePanel that I've placed in my site_code directory. I am using a Web App not Web Site so App_Code is not really available to me.

Namespace webstation.WebControls

    Public Class CollapsablePanel
        Inherits System.Web.UI.WebControls.Panel


    End Class

End Namespace

2) In the .aspx file I've added <%@ Register TagPrefix="webstation" Namespace="MyApplication.webstation.WebControls" %>

I've built the project but my custom tag prefix does not appear. If I just go ahead and type the editor does not throw an error, however the page does when I publish it and try to access it. If I try to access the class from the codebehind (Imports MyApplication.webstation.WebControls) the custom control appears in intellisense, so I know that Visual Studio is reading the class information to some extent.

What am I doing wrong here? Any thoughts are greatly appreciated.

Mike

+2  A: 

seems like you may be missing the TagName attribute as

<%@ Register TagPrefix="webstation" TagName="CollapsiblePanel" Namespace="MyApplication.webstation.WebControls" %>

once you do this you should be able to access it as

<webstations:CollapsiblePanel id='usercontrol1" runat="server" />
Yasir Laghari
I'm not sure I should have to have the TagName as I would like it to provide all classes that I will have in the Namespace without having to register each one individually.Plus, the tutorial video on ASP.net does not use a TagName either:http://www.asp.net/learn/videos/video-297.aspx
Mike C
If you want to register *all* of the classes in a given Namespace, I believe you need to provide the assembly attribute in your @Register directive. So something like Assembly="MyApplication.webstation.WebControls", assuming that WebControls is a project that generates a DLL as output.
Chris Shouts
You're right MIke, you don't need to specify the tagname but you do need to specify the assembly. I just tried it and it worked after I added the assembly attribute for my dll
Yasir Laghari
I've tried using the Assembly attribute as well with no luck. This control is not currently part of a class library or compiled DLL, it's merely a .vb file included in the project. I intend to move towards using a class library once if / when I develop more controls, but for now that feels like overkill since I've only got one control and I'm only using it in one project. Does it HAVE to be in a class library for it to work within a Web Application?
Mike C
@ Mike You should read the MSDN docs on this - it covers all the possible cases. http://msdn.microsoft.com/en-us/library/c76dd5k1.aspx
Chris Shouts
+1  A: 

Check out Scott Gu's blog post on registering controls, I like registering them in the web.config file myself. http://weblogs.asp.net/scottgu/archive/2006/11/26/tip-trick-how-to-register-user-controls-and-custom-controls-in-web-config.aspx

You need to make sure you have a fully qualified reference to the control class, meaning the library name and namespace. I place my controls in a class library, but you can include them in your App_Code folder. You can also register user controls in the web.config, both examples follow:

Chris Love
A: 

I was able to get it work like expected once I went ahead and created a Class Library project in the same solution, built the project, copied the DLL from the bin of the Class Library and placed it in a folder of my Web Project used for holding external binaries. I also added a reference to the .dll in the project. Once I did that, then the syntax:

<%@ Register Assembly="webstation.WebControls" Namespace="webstation.WebControls" TagPrefix="webstation" %>

began to work. Does anyone know if I am able to somehow automatically update the compiled .DLL in my web project from the class library in the same solution? Or do I just have to go into the bin folder each time and manually copy it into the web project?

Thanks,

Mike

Mike C