Click here to Skip to main content
15,036,145 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to create rich design time control for ASP.NET in Visual Studio 2013(2). Unfortunately I can't get to work even simple property attributes as Description. Especially I need to add custom editor to Visual PropertyGrid (when you select control in designer or source of asp.net file and press F4). I managed to get this working 1 for hundred tries, completely unrelated as far as I can tell. For example when I created new control, with almost the same code, add it to the page, build and try F4. Or after adding new completely unrelated property to control or swapping base class of control. Even when this work, any change to code, build and another try break previous working solution and you cannot go back to it because it do not work anymore. As far as I can tell (after some research) you can't debug those editors, developing it is like walking in complete dark.

Code below compiles and runs in a browser. To test it there is need to add System.Windows.Forms (?!) and System.Drawing references.

Control code:

C#
using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Globalization;
using System.Web.UI;
using System.Windows.Forms.Design;
using editors;

namespace WebApplication1.Controls
{
    public class UC : Control
    {
        [Browsable(true)]
        [Description("test prop")]
        [Editor(typeof(Editor), typeof(UITypeEditor))]
        public string test1 { get; set; }
    }
}

namespace editors
{
    public class Editor : UITypeEditor
    {
        private IWindowsFormsEditorService editorService;

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            value = "test1";
            if (provider != null)
            {
                editorService =
                    provider.GetService(
                    typeof(IWindowsFormsEditorService))
                    as IWindowsFormsEditorService;
            }
            if (editorService != null)
            {
                var listBox = new System.Windows.Forms.ListBox();
                listBox.Items.Add("test");
                listBox.SelectedValueChanged += listBox_SelectedValueChanged;
                editorService.DropDownControl(listBox);

                value = listBox.SelectedItem;
            }
            return value;
        }

        void listBox_SelectedValueChanged(object sender, EventArgs e)
        {
            if (editorService != null)
            {
                editorService.CloseDropDown();
            };
        }
    }
}



Web forrm code:
XML
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Register TagPrefix="uc" Namespace="WebApplication1.Controls" Assembly="WebApplication3" %>

<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        test
        <uc:UC runat="server"></uc:UC>
    </div>
    </form>
</body>
</html>



Web config:

XML
<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5">
        <assemblies>
          <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
          <add assembly="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </assemblies>
      </compilation>
      <httpRuntime targetFramework="4.5" />
    </system.web>

</configuration>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900