Thursday, March 31, 2011

Visual Studio 2008 user controls registered in the web.config not registering with intellisense

I have a C# Web application built in Visual Studio 2008 where we rely heavily on user controls for 'pre-built' blocks of ASP.NET.

Instead of registering a big stack of user controls on each page, we register all the controls in a local (to a specific folder) web.config file. The advantage is that we can use the controls and the pages look 'cleaner' in source view. However, neither the VS2008 Design view nor Intellisense recognize the fact the controls are registered in web.config. However, the application itself works as expected.


Normally at the top of each page we'd have a tag like this:

<%@ Register src="~/CommonControls/Foo.ascx" tagname="Foo" tagprefix="Bar" %>

And we register the controls in a local web.config like so:

<?xml version="1.0"?>
<configuration>
    <system.web>
        <pages>
            <controls>
                <add src="~/CommonControls/Foo.ascx" tagName="Foo" tagPrefix="Bar"/>
            </controls>
        </pages>
    </system.web>
</configuration>

Does anyone know of a fix for getting Intellisense to recognize these custom controls? Or, is there a 'better' way of doing this?

From stackoverflow
  • It probably has to do with the fact that it isn't registered in the main web.config file. Test it out by putting one or two references in the main web.config file.

    Mike McClelland : Hi there. Thanks for your answer. Yes, it does work in the main web.config but I expected this to work in local web.config. Surely, this is expected behaviour. Any ideas? Mike
  • Could it because you are registering the control twice and intellisense is unable to resolve the reference?

    I would exepect that the control should be registered in either a config file or in the page directive

    Mike McClelland : Hi Blatfrig. Thanks for your suggestion. The question is slightly ambigious. To clarify: we didn't register it twice, the two code snippets above were alternatives that should work. I agree doing them together would cause confusion! - Mike
  • I tried your sample code and Intellisense works in both the code view of the aspx page and the code behind page. You might check the class names of your user controls, visual studio includes the folder name in the class name by default. So your control may be named CommonControls_Foo instead of Foo as you would expect.

    Either way the web.config file has nothing to do with the Intellisense of the code behind. Check the name and namespace of the class.

  • Whilst this doesn't work in a local Web.Config file you can use the Location tag in your root web.config file to achieve the same result.

    For example:

    <?xml version="1.0"?>
    <configuration>
        <location path="MyPath">
            <system.web>
                <pages>
                    <controls>
                        <add src="~/CommonControls/Foo.ascx" tagName="Foo" tagPrefix="Bar"/>
                    </controls>
                </pages>
            </system.web>
        </location>
    </configuration>
    
  • "Could it because you are registering the control twice and intellisense is unable to resolve the reference?

    I would exepect that the control should be registered in either a config file or in the page directive"

    y that´s why. Thx :)

0 comments:

Post a Comment