locked
Does VS2015 support creating custom ASP.NET Server Controls? RRS feed

  • Question

  • User-1826179050 posted

    I have been trying for the last day or two to create a custom server control inheriting from System.Web.UI.WebControls.TextBox using VS2015. There is no project type for ASP.NET Server Control listed in the project types as there is in my VS2012 instance. I have been trying to use the Project type for a simple Class Library. I have not created a custom server control before, but it's acting like there are some references or something that are not right while trying to use the Class Library project to try to create a customer Server Control. In examples I have found, the use of ToolboxData is not being recognized by Intellisense. Intellisense does not recognize any further sub-namespaces below System.Web.  I can't add the control to the Toolbox, etc...  I don't know if it's a support issue, a project type issue, a coding issue, etc... My thinking is that the proper project type is the core of the issue.

    If I'm off track here, any references (or help) in creating a custom textbox control derived from the basic ASP.NET TextBox control would be greatly appreciated - using VS2015.

    Thanks!

    Monday, July 18, 2016 2:19 PM

Answers

  • User-1826179050 posted

    Ok, so I kind of solved my problem. I created an ASP.NET Server Control project in VS2012 so I could compare project references, using statements, frameworks being targeted, etc... In the VS2012 project - Intellisense is working as expected. If I add "using System.Web." in the .cs file, the dropdown shows all of the subnamespaces under System.Web as expected, and the same when "System.Web.UI." is typed, all the subnamespaces show - including WebControls - none of this works in the VS2015 project. In VS2015, "Sysem." will show subnamespaces - including Web, but "System.Web." does nothing. It's like it has no idea that there are subnamespaces below "System.Web". In the VS2012 project, the ToolboxData attribute is recognized and the text is in the cyan color like normal - but not in the VS2015 project. I simply cannot figure out why the VS2015 .cs file is not working right. I don't think Intellisense is simply not working - it's like it doesn't see classes in the namespaces below System.Web - even though I have included the same usings and references. Thus, it just doesn't work.

    Basically, I found no difference between the projects. The frameworks targeted were 4.5.2 in VS2015 and 4.5 in VS2012. However, changing my VS2015 project to target 4.5 (and unloading/reloading the project) changed nothing. In the VS2015 project - all the references listed in the VS2012 project were there. It just refuses to work or to allow me to include the class in the Toolbox - stating there are not controls in the dll.

    So, I decided to add the .cs file from my VS2012 test project to the VS2015 project just to see what would happen. Low and behold - it worked fine! Intellisense is picking up the namespaces below System.Web as expected. The ToolboxData attribute is being recognized and color coded as expected. I even copied everything below my using statements in the original - nonworking VS2015 .cs file and pasted it over the same area of the new file - and again it all works fine. The using statements were identical between the files. At this point Intellisense is even showing some of the using statements as not even needed (I assume because of references added to the project?). I don't get this. No change in the AssemblyInfo.cs file, no project property changes. Basically pulled in another .cs file, copied the content of my original .cs file into the new .cs file, and it works. This makes no sense to me. I have now been able to add the new control to my Toolbox and am using it in my intended project. I have no idea why this worked or what was wrong. But I'm functional now.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 19, 2016 1:21 PM

All replies

  • User-1826179050 posted

    Let me give a little more information. I am maintaining a web site that was created as a ASP.NET Web Form Application. In Visual Studio 2008 or 2010. I have maintained it under VS2012 and 2013. But we now have a need for a custom ASP.NET Server Control to use in this web application. Can Visual Studio 2015 create ASP.NET Server Controls? There was a project type under VS2012 for ASP.NET Server Control, but I cannot find this option for a project type under VS2015. I have searched for how to create one in VS2015 and can find nothing. I have found many examples of creating one - but they are all using the ASP.NET Server Control project type - and all appear old enough that they are clearly pre-VS2015.

    One of the attributes these samples I have found use the ToolboxData attribute - and in their examples it is color coded (the code pasted below is highlighted in cyan, but on my system - it is not). Mine does not. Another attribute is DefaultProperty. Again in the examples, this is color coded - in mine it is picked up and color coded properly - which is also partly why I think there is a project type issue where references are not being setup.

    As of right now, I have used a Class Library Project to create this custom server control and here is the content of my myWebControls.cs file:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace myWebControls
    {
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:mlLabel ID='mlLabelID' runat=server></{0}:mlLabel>")]
    
        public class mlLabel:System.Web.UI.WebControls.TextBox
        {
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue("")]
            [Localizable(true)]
    
            //Set these properties for a textbox to make a 'multiline' label
            //    rows = {some number greater than 0} (I use the rows property rather than the height property as it tends to be earier to get the formatting right)
            //    ReadOnly = true
            //    TextMode = multiline
            //    Wrap = true;
            //    BorderStyle = None
            //    BorderWidth = 0
    
            protected mlLabel()
            {
                Rows = 1;
                ReadOnly = true;
                TextMode = System.Web.UI.WebControls.TextBoxMode.MultiLine;
                Wrap = true;
                BorderStyle = System.Web.UI.WebControls.BorderStyle.None;
                BorderWidth = 0;
            }
    
            //protected override void RenderContents(HtmlTextWriter op)
            //{
            //    op.Write(ch);
            //    base.RenderContents(op);
            //}
        }
    }

    Also, there are only two lines added (commented as such) in the AssemblyInfo.cs file:

    using System.Web.UI; // This line added by me
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    
    // General Information about an assembly is controlled through the following 
    // set of attributes. Change these attribute values to modify the information
    // associated with an assembly.
    [assembly: AssemblyTitle("myWebControls")]
    [assembly: AssemblyDescription("")]
    [assembly: AssemblyConfiguration("")]
    [assembly: AssemblyCompany("")]
    [assembly: AssemblyProduct("myWebControls")]
    [assembly: AssemblyCopyright("Copyright ©  2016")]
    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyCulture("")]
    [assembly: TagPrefix("myWebControls", "my")]  // This line added by me
    
    // Setting ComVisible to false makes the types in this assembly not visible 
    // to COM components.  If you need to access a type in this assembly from 
    // COM, set the ComVisible attribute to true on that type.
    [assembly: ComVisible(false)]
    
    // The following GUID is for the ID of the typelib if this project is exposed to COM
    [assembly: Guid("7a15717c-c58a-4966-ac71-dd9fc2f36e2c")]
    
    // Version information for an assembly consists of the following four values:
    //
    //      Major Version
    //      Minor Version 
    //      Build Number
    //      Revision
    //
    // You can specify all the values or you can default the Build and Revision Numbers 
    // by using the '*' as shown below:
    // [assembly: AssemblyVersion("1.0.*")]
    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]
    

    I also added a reference to the project for System.Web.

    Again, the ToolboxData attribute does not appear to be recognized as it is not color coded. Also, when I compile the project and try adding the new control to the Toolbox (right clicking, Choose Item...) it is not in the list of items to add. I have copied the myWebControls.dll file into the bin folder of the ASP.NET web application and added a reference to the file, but Toolbox still does not list it. So, I tell the Toolbox to browse for the file where I select it. But it then says: "There are no components in [removed path to project]\myWebControls.dll that can be placed on the toolbox."

    I have searched for this error - finding more examples of how to do this in pre- VS2015 that don't seem to make any difference.

    What am I doing wrong?

    Thanks!

    Monday, July 18, 2016 6:04 PM
  • User-1826179050 posted

    Ok, so I kind of solved my problem. I created an ASP.NET Server Control project in VS2012 so I could compare project references, using statements, frameworks being targeted, etc... In the VS2012 project - Intellisense is working as expected. If I add "using System.Web." in the .cs file, the dropdown shows all of the subnamespaces under System.Web as expected, and the same when "System.Web.UI." is typed, all the subnamespaces show - including WebControls - none of this works in the VS2015 project. In VS2015, "Sysem." will show subnamespaces - including Web, but "System.Web." does nothing. It's like it has no idea that there are subnamespaces below "System.Web". In the VS2012 project, the ToolboxData attribute is recognized and the text is in the cyan color like normal - but not in the VS2015 project. I simply cannot figure out why the VS2015 .cs file is not working right. I don't think Intellisense is simply not working - it's like it doesn't see classes in the namespaces below System.Web - even though I have included the same usings and references. Thus, it just doesn't work.

    Basically, I found no difference between the projects. The frameworks targeted were 4.5.2 in VS2015 and 4.5 in VS2012. However, changing my VS2015 project to target 4.5 (and unloading/reloading the project) changed nothing. In the VS2015 project - all the references listed in the VS2012 project were there. It just refuses to work or to allow me to include the class in the Toolbox - stating there are not controls in the dll.

    So, I decided to add the .cs file from my VS2012 test project to the VS2015 project just to see what would happen. Low and behold - it worked fine! Intellisense is picking up the namespaces below System.Web as expected. The ToolboxData attribute is being recognized and color coded as expected. I even copied everything below my using statements in the original - nonworking VS2015 .cs file and pasted it over the same area of the new file - and again it all works fine. The using statements were identical between the files. At this point Intellisense is even showing some of the using statements as not even needed (I assume because of references added to the project?). I don't get this. No change in the AssemblyInfo.cs file, no project property changes. Basically pulled in another .cs file, copied the content of my original .cs file into the new .cs file, and it works. This makes no sense to me. I have now been able to add the new control to my Toolbox and am using it in my intended project. I have no idea why this worked or what was wrong. But I'm functional now.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 19, 2016 1:21 PM