views:

142

answers:

2

I am feeling like an idiot right now, but I cannot for the life of me figure out why I cannot seem to call user controls from within my asp.net webpage. I'm still learning asp.net but I can't find any information from searching on google.

I'm trying to load a specific control on the page when the user presses a linkbutton. So I created an empty user control via the right click menu:

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

In other words, I have not touched any part of the created user control. Yet attempting to create this web control and add it to the form seems to not work, as it claims that the WebUserControl class does not exist (I have no other controls in my project):

UserControl blah = new WebUserControls();

produces a "The type or namespace is invalid". Why can none of my asp.net webform pages get the control into scope?

+2  A: 

The new control must be added to the site's Web.config file.

<configuration>
  <system.web>
    <pages>
      <controls>
        <add tagPrefix="my"
             tagName="WebUserControl"
             src="~/WebUserControl.ascx"/>
      </controls>
    </pages>
  </system.web>
</configuration>

Use this to place the new control in an .aspx page.

<my:WebUserControl runat="server" ID="MyWebUserControl" />
Vadim K.
That seemed to have fixed it, thanks! Is there a way to do that for all controls in my Control directory, or do I have to do each ascx individually?
KallDrexx
Each and every one, I'm afraid.
Vadim K.
Actually, I seem to be having issues with this. It works fine in the aspx file (I can add <my:WebUserControl> tags fine, but in code the aspx.cs file it is not recognizing the WebUserControl type.
KallDrexx
I assume you're using Visual Studio and this sounds like it may be related to the type of project you're using. If you created an "ASP.NET Web Site" (File/New/Web Site), things should just work. The other kind, "ASP.NET Web Application" (File/New/Project), was designed to enable Visual Studio 2003 style ASP.NET development and is unlikely to be what you want to use without a specific reason.
Vadim K.
Yeah I'm using VS2008 and my project is just a web site, not a web application. Even adding a <% Register or <% Reference to the page isn't allowing me to use those controls programmatically, except sporadically (and I can't determine why it sometimes will and sometimes won't).
KallDrexx
Bizarre. What directory are the files for the user control in? Have you tried placing them at the top level of the site (just to see if it works)? Are you using IIS?
Vadim K.
Just to double-check the basics, does the WebUserControl.ascx.cs file actually contain a public partial class named WebUserControl which derives from UserControl?
Vadim K.
A: 

In addition to registering them one-by-one in web.config, you can also use a <%@ Register %> directive in the source of the markup files that reference the control.

Or, you can move your controls into a DLL and include them all by referencing the assembly from your web.config (takes some extra work, though).

RickNZ