locked
How do I refer to the Control? RRS feed

  • Question

  • User-511826081 posted

    Hello folks,

    I have an overloaded control. 

    Not much - just few properties adjusted to interact with my classes.

    When control are in DLL - there are no problem:

    < % @ Register TagPrefix="CTRL" Namespace="MyNamespace.Controls" Assembly="MyDllWithControls" % >
    < CTRL:SitesDropDownList ID="sites" runat="server" > < / CTRL:SitesDropDownList >

    and control available.

    For quick modification I would like to have control on the same site - in App_Code folder.

    How do I refer to this control? 

    Friday, July 12, 2019 3:10 PM

All replies

  • User-2010311731 posted

    If you are using Visual Studio, switch your page to Design View.  From the Solutions Explorer window, drag and drop the control file onto your page, and it will automatically set the references for you.

    Friday, July 12, 2019 3:53 PM
  • User839733648 posted

    Hi Andrey Belyakov,

    Andrey Belyakov

    When control are in DLL - there are no problem:

    < % @ Register TagPrefix="CTRL" Namespace="MyNamespace.Controls" Assembly="MyDllWithControls" % >
    < CTRL:SitesDropDownList ID="sites" runat="server" > < / CTRL:SitesDropDownList >

    and control available.

    For quick modification I would like to have control on the same site - in App_Code folder.

    According to your description, I'm sorry that I'm not so clear about your issue.

    You say that the control is in DLL.

    In that case, you just have to register the control with the three attributes: Assembly, Namespace, TagPrefix.

    As long as your folder, your page file and the dll are in the same project, you could refer to the control in the same way.

    Reference: https://support.microsoft.com/en-us/help/321749/how-to-register-an-assembly-in-a-webform-to-use-a-custom-control 

    Best Regards,

    Jenifer

    Monday, July 15, 2019 6:36 AM
  • User-511826081 posted

    You say that the control is in DLL.

    -----

    I sad that I do not have problem to refer to the control when control are in DLL.

    Question is - If I remove DLL from the project and put control code into App_Code folder - How do I refer to the control in App_Code folder? 

    Monday, July 15, 2019 7:58 AM
  • User-511826081 posted

    > switch your page to Design View.

    Sorry, my Studio didn't switch into Design View after few crash and fixing.

    Where and what will be written after those manipulation? 

    Monday, July 15, 2019 8:01 AM
  • User839733648 posted

    Hi Andrey Belyakov,

    Question is - If I remove DLL from the project and put control code into App_Code folder - How do I refer to the control in App_Code folder? 

    You may do the following steps:

    1. Create a new Empty Website (ASP.Net).
    2. Add your control code to the App_Code folder. For example MyControl.cs.
      namespace CustomControls
      {
         public class MyControl : System.Web.UI.WebControls.MyControl
         {
         }
      }
    3. Register the assembly/namespace for app_code in your aspx page:
      <%@ Register Namespace="CustomControls" Assembly="__code" tagprefix="CTRL" %>
    4. Add an instance of the new control to your page:
    <CTRL:MyControl ID="sites" runat="server" > </CTRL:MyControl>

    For more, you could refer to this link which contains the more detailed description: https://stackoverflow.com/a/4605805/10487763 

    Best Regards,

    Jenifer

    Monday, July 15, 2019 9:42 AM
  • User-511826081 posted

     <%@ Register Namespace="CustomControls" Assembly="__code" tagprefix="CTRL" %>

    -----

    This was an initial cause of the question - already try this and it didn't work.

    Even if I wrap control code in namespace or remove 'CustomControls' (to "") or replace namespace from class  - didn't work - control not found.

    Any specific configuration for IIS 7.0 or IIS 10.0 Express or Studio to have this working? 

    OR

    What need to be placed as Namespace and Assembly? 

    Monday, July 15, 2019 2:15 PM
  • User-511826081 posted

    Ohhh, didn't see the link. Will check tomorrow. Thanks.

    Monday, July 15, 2019 3:14 PM
  • User-511826081 posted

    Still not able to refer to the simple control in App_Code.

    Every time have an error at run-time. 

    Error message - varied, depend on syntax used in Register directive.

    Control code in App_Code folder:

        public class DummyControl3 : System.Web.UI.WebControls.Label
        {
            const string LabelText = "This is a Dummy Label from App_Code folder.";
    
            public DummyControl3() : base() { }
    
            public override string Text { get { return LabelText; } }
        }

    Registration 

    <%@ Register tagPrefix ="CTRL" namespace="OneFormWebApplication.CTRL3" Assembly="__code" %>

    Parser Error Message: The base class includes the field 'Label2', but its type (OneFormWebApplication.CTRL3.DummyControl3) is not compatible with the type of control (OneFormWebApplication.CTRL3.DummyControl3).

    Registration:

    <%@ Register tagPrefix ="CTRL" TagName="DummyControl4" src="~/App_Code/DummyControl3.cs" %>

    Parser Error Message: The file '/App_Code/DummyControl3.cs' is in the special directory 'App_Code', which is not allowed.

    Registration :

    <%@ Register tagPrefix ="CTRL" namespace="OneFormWebApplication.CTRL3"  Assembly="OneFormWebApplication"%>

    Compiler Error Message: CS0433: The type 'DummyControl3' exists in both 'App_Code.5whj1ddz, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' and 'OneFormWebApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

    Question still open - How to refer to the control in App_Code folder? 

    Wednesday, July 24, 2019 9:41 AM
  • User839733648 posted

    Hi Andrey Belyakov,

    I've tried your code and it seems work well. You may try to move the Inherits as below.

    TestControl.aspx

    <%@ Register tagPrefix ="CTRL" namespace="WebApplication1.App_Code" Assembly="__code" %>
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestControl.aspx.cs" Inherits="WebApplication1.TestControl" %>
    
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <CTRL:DummyControl3 ID="sites" runat="server" > </CTRL:DummyControl3>
            </div>
        </form>
    </body>
    </html>
    

    DummyControl3.cs in App_Code

    namespace WebApplication1.App_Code
    {
        public class DummyControl3 : System.Web.UI.WebControls.Label
        {
            const string LabelText = "This is a Dummy Label from App_Code folder.";
    
            public DummyControl3() : base() { }
    
            public override string Text { get { return LabelText; } }
        }
    }

    result:

    Best Regards,

    Jenifer

    Thursday, July 25, 2019 6:40 AM