How to register custom server control on ASP.NET page

前端 未结 4 1191
忘掉有多难
忘掉有多难 2020-12-31 00:25

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

相关标签:
4条回答
  • 2020-12-31 00:49

    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.

    0 讨论(0)
  • 2020-12-31 01:09

    Add an assembly attribute to your register tag

    0 讨论(0)
  • 2020-12-31 01:11

    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!

    0 讨论(0)
  • 2020-12-31 01:12

    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" ... />
    
    0 讨论(0)
提交回复
热议问题