5

Is it possible to register a complete namespace of usercontrols in an aspx-File, instead of each control seperately?

I have created a bunch of usercontrols and collected them into an own namespace "MyWebControls", like this:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LevelFilter.ascx.cs" Inherits="MyWebControls.LevelFilter" %>

Codebehind:

namespace MyWebControls
{
    public partial class LevelFilter : System.Web.UI.UserControl
    {
       ...
    }
}

What I tried now to include them in my pages (and what did not work):

<%@ Register Namespace="MyWebControls" TagPrefix="ucs" %>
...
<ucs:LevelFilter />

Is there any way to do this? Apparently it works with external assemblies like AjaxControlToolkit, so I guess this should be possible.

I am using ASP.NET 4.0.

magnattic
  • 11,529
  • 10
  • 59
  • 109
  • You can only use the assembly and namespace attributes when you're registering server controls that are compiled into an assembly :-( – PhilPursglove Mar 03 '11 at 16:16
  • Okay, I see that it isnt working this way. But is there another way maybe? – magnattic Mar 03 '11 at 16:18
  • 1
    @atticae You can register your usercontrols in the web.config so they're available across all your pages instead of registering them in each page seperately. That's the only thing I can think of right now that might help you out. – PhilPursglove Mar 03 '11 at 16:21
  • I started a bounty in the hope that someone knows a neat way to register a whole namespace of controls at once. If there isnt, bad luck. I just think it's worth another shot, because it would be a helpful feature. – magnattic Mar 09 '11 at 14:14
  • if your controls are in .ascx files, there is no other way than to register your controls one by one. you could use something like T4 though to automatically generate the Register-tags for your from the content of certain folders. – Pauli Østerø Mar 09 '11 at 14:19
  • @PhilPursglove: Okay, so it seems to be impossible and you were the first one who got pointed it out correctly. Create an answer and I'm gonna award you with the bounty. – magnattic Mar 13 '11 at 22:38

2 Answers2

6

With user controls there's just no way to do this :-( You can only use the namespace and assembly attributes to bring in controls from an assembly, and usercontrols don't export to a separate assembly very well (I suspect it's to do with the way user controls have separate code and markup).

If you really have your heart set on this you'll need to convert your user controls to server controls - there's a piece on the CodeProject here that looks like it might offer some shortcuts to doing this.

Otherwise my best suggestion is to register all your user controls centrally in your web.config so they are available to all your pages. To do this, in your web.config under system.web/pages/controls add each of them like this:

<add tagprefix="ucs" tagname="MyFirstControl" src="~/UserControls/MyFirstControl.ascx" />
PhilPursglove
  • 12,371
  • 5
  • 43
  • 63
5

For Global Registration

In your web.config under system.web/pages/controls

<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.
    <add tagPrefix="ucs" namespace="MyWebControls" assembly="MyAssemblyName" />
  </controls>

Documentation for this part of your web.config in .NET 4.0 is available at msdn.microsoft.com/en-us/library/ms164640.aspx .

For Local Registration

The reason your @Register directive is not working is that you have omitted the assembly attribute. The line should look like

<%@ Register TagPrefix="ucs" Namespace="MyWebControls" Assembly="MyAssemblyName" %>

Please see this related post for details.

Community
  • 1
  • 1
smartcaveman
  • 38,142
  • 26
  • 119
  • 203
  • could you provide an example please how i can register the whole namespace there? – magnattic Mar 03 '11 at 19:01
  • And what would I put in "assembly"? My controls are not in a separate assembly, they are in the ASP.NET app itself. Pretty much the same problem there as if I put it in the page directly. – magnattic Mar 03 '11 at 19:25
  • go to your project properties, copy and paste the assembly name – smartcaveman Mar 03 '11 at 19:25
  • Again, can you be more specific please. I dont see any assembly name in the project properties of my ASP.NET Website. Where exactly would I find it? – magnattic Mar 03 '11 at 21:40
  • Copy and paste your web.config. – smartcaveman Mar 03 '11 at 22:12
  • Its a default config, no special changes apart from connection string. Definitly no assembly name in there. – magnattic Mar 09 '11 at 14:11
  • if your controls are in .cs/.vb files in your app_code folder you should be able to register your namespaces using Web as assembly, since thats the assembly-name these code-files will be compiled into. – Pauli Østerø Mar 09 '11 at 14:18
  • The default config has namespace registrations for the user controls. That's how the default ones like System.Web.UI get there so you don't need to include them on a page-by-page basis – smartcaveman Mar 09 '11 at 14:21
  • Unfortunately they I am not using custom controls, but user controls, so they aren't in the app_code folder. – magnattic Mar 09 '11 at 14:22
  • @smartcaeman: Sure you are talking about .NET 4.0? They changed a lot in the web.config and moved default stuff out of there. At least I have no namespace registrations in my default config at all. – magnattic Mar 09 '11 at 14:24
  • @atticae, Yes I am. The MSDN documentation is available at http://msdn.microsoft.com/en-us/library/ms164640.aspx . As you can see, it says **.NET Framework 4.0** at the top. – smartcaveman Mar 12 '11 at 12:01
  • Yeah, but System.Web is in another assembly. Im not really sure you understood my problem, at least your answer doesn't solve it because my controls ARE NOT in a separate assembly, but in the Website project itself. I really dont know why this got upvoted... – magnattic Mar 12 '11 at 12:47
  • It doesn't matter if they are in the same assembly. Your web.config is not compiled in any assembly, and neither are your .aspx or .ascx files. Why don't you try adding the Assembly attribute, like I suggested, before you criticize a working solution. You're not going to get a better answer because this is the correct one. Peace. – smartcaveman Mar 12 '11 at 12:51
  • I would, but you still haven't told me what value to put in the assembly attribute? I tried the name of the application, the name of the namespace and leaving it blank, none of it worked. I would gladly award you the bounty if your solution works, but so far you havent provided me with the information to make it work, or am I missing something here? – magnattic Mar 13 '11 at 13:57
  • To specify my question: What to put in into the assembly attribute?? - There is no assembly, so telling me to put in the assembly name is kind of not helpful? – magnattic Mar 13 '11 at 13:59
  • @atticae, To get the name of the assembly associated with your current project, Right Click the Properties folder and select Open. You will see an input field labeled "Assembly name". Copy and paste the text from that input field. – smartcaveman Mar 13 '11 at 14:00
  • There is no properties folder, and neither in the Property Window nor in the Property Pages of my WebSite Project is an input field named "assembly". Thats what I've been trying to tell you. I am using VS2010 and it's an ASP.NET Web Site. (not MVC! not Web Application!) – magnattic Mar 13 '11 at 14:06
  • Oh okay. (1) If it is a user control, then use a Reference directive instead of Register http://msdn.microsoft.com/en-us/library/w70c655a.aspx . (2) If it is a server control, then create a C# library project, move the files there and reference the project in your website – smartcaveman Mar 13 '11 at 14:19
  • Okay, so basically you are also telling me that what I want to achieve is not possible. Well than, gonna do it the oldfashioned way, registering them one by one. – magnattic Mar 13 '11 at 22:29