How to register custom server control on ASP.NET page

37,946

Solution 1

Well, if this control is in another class library, or even if it's in the same one, it wouldn't be a bad idea to specify control's assembly in @Register:

<%@ Register TagPrefix="myControls" Namespace="MyApp.Controls" Assembly="MyApp" %>
<myControls:CustomControl runat="server" Text="What's up!" />

Clean and rebuild your solution too in order to verify everything is compiled rightly!

Solution 2

If your control will be reused on several pages, you may want to register it in web.config, as one of system.web/pages/controls subelements instead of copy-pasting the same <@Register tag in all affected pages.

web.config:

<system.web>
  <pages ...>
    <controls>
      ...
      <add tagPrefix="myCompany" namespace="MyCompany.Whatever.Controls" assembly="Whatever"/>
    </controls>

thepage.aspx:

<myCompany:ControlClassName ID="TheStuff" runat="server" ... />

Solution 3

You should put your control either under the App_Code folder (in the case if the control not in assembly) or add a reference to assembly where this control is:

<%@ Register TagPrefix="myControls" Namespace="MyApp.Controls"
      Assembly="SomeAssembly" %>

But guessing, your control not under the App_Code folder.

Solution 4

Add an assembly attribute to your register tag

Share:
37,946

Related videos on Youtube

smartcaveman
Author by

smartcaveman

Does software exist? https://www.codementor.io/smartcaveman

Updated on November 12, 2021

Comments

  • smartcaveman
    smartcaveman over 2 years

    I have a project and I am trying to register a custom server control (there is no .ascx file) on the page. I am currently using

    Class Declaration

    namespace MyApp.Controls{
        public class CustomControl: WebControl{
            public string Text
            {
                get
                {
                    String s = (String)ViewState["Text"];
                    return ((s == null) ? String.Empty : s);
                }
                set
                {
                    ViewState["Text"] = value;
                }
            }        
            protected override void RenderContents(HtmlTextWriter output)
            {
                output.Write(Text);
            }
        }
    }
    

    On my page,

    <%@ Register TagPrefix="myControls" Namespace="MyApp.Controls" %>
    <myControls:CustomControl runat="server" Text="What up!" />
    

    I receive a Parser Error, with the message "Unknown server tag 'myControls:CustomControl'."

    What am I doing wrong?

    • PMC
      PMC about 13 years
      I think you need assembly too.
  • Cosmin
    Cosmin about 13 years
  • smartcaveman
    smartcaveman about 13 years
    I got it to work by adding the Assembly attribute. Placing the code file in App_Code was not necessary
  • Matías Fidemraizer
    Matías Fidemraizer about 13 years
    No problem! Great to know you got it :)
  • CookieEater
    CookieEater over 10 years
    Just wondering, why using Src instead of Assembly does not work? Does this mean I need to compile my source and put the dll in bin folder? Src works with user control.
  • Matías Fidemraizer
    Matías Fidemraizer over 10 years
    @CookieMonster Well, you don't put the DLL in the bin folder manually.... Do you imagine using Src in order to tell that a regular C# class is the control? And what about the dependencies, where are coming from? Using Src would defeat the point of coding server controls.
  • Eccentropy
    Eccentropy over 8 years
    If it still doesn't work, you may need to specify the strong name for the assembly, which you can get by examining the assembly in the GAC. Example: assembly="MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5499f3feedc0ffee" instead of only assembly="MyApp".