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 f开发者_运维知识库ile 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
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" />
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:
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
精彩评论