Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. custom server control problem

custom server control problem

Scheduled Pinned Locked Moved ASP.NET
helpcsharpvisual-studiosysadmindebugging
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jurgen Muller jmse
    wrote on last edited by
    #1

    Hi, all. I'm having a strange problem while writing custom server control. The control works fine so far, but I wanted to add some features which support the developer while using the control in the Visual Studio designer. Therefore I've added a designer which extends ContainerControlDesigner... Code fragment: public class MyDesigner : ContainerControlDesigner { public override string FrameCaption { get { MyComponent c = (MyComponent)base.Component; return "MyComponent - " + c.MyProperty; } } Problem: Sometimes the cast MyComponent c = (MyComponent)base.Component will throw an exception - and sometimes not. I haven't found a reason for this. The type of base.Component is MyComponent. I've checked this in the debugger. Even Visual Studio shows me all properties of this type during debugging. I've modified the code and read the value of c.MyProperty via reflection - and it works... Any ideas what might be wrong or what happens here? :confused: Any help appreciated. Regards Jürgen Stuttgart Germany

    J M 2 Replies Last reply
    0
    • J Jurgen Muller jmse

      Hi, all. I'm having a strange problem while writing custom server control. The control works fine so far, but I wanted to add some features which support the developer while using the control in the Visual Studio designer. Therefore I've added a designer which extends ContainerControlDesigner... Code fragment: public class MyDesigner : ContainerControlDesigner { public override string FrameCaption { get { MyComponent c = (MyComponent)base.Component; return "MyComponent - " + c.MyProperty; } } Problem: Sometimes the cast MyComponent c = (MyComponent)base.Component will throw an exception - and sometimes not. I haven't found a reason for this. The type of base.Component is MyComponent. I've checked this in the debugger. Even Visual Studio shows me all properties of this type during debugging. I've modified the code and read the value of c.MyProperty via reflection - and it works... Any ideas what might be wrong or what happens here? :confused: Any help appreciated. Regards Jürgen Stuttgart Germany

      J Offline
      J Offline
      John Petersen
      wrote on last edited by
      #2

      Hello Jürgen Im not sure why you get this error, but propably the type is not right. Use a type safe cast instead and call the base class if the cast is not successful. public class MyDesigner : ContainerControlDesigner { public override string FrameCaption { get { MyComponent c = base.Component as MyComponent; if (c != null) return "MyComponent - " + c.MyProperty; else return base.FrameCaption; } } Kind Regards, John Petersen

      J 1 Reply Last reply
      0
      • J John Petersen

        Hello Jürgen Im not sure why you get this error, but propably the type is not right. Use a type safe cast instead and call the base class if the cast is not successful. public class MyDesigner : ContainerControlDesigner { public override string FrameCaption { get { MyComponent c = base.Component as MyComponent; if (c != null) return "MyComponent - " + c.MyProperty; else return base.FrameCaption; } } Kind Regards, John Petersen

        J Offline
        J Offline
        Jurgen Muller jmse
        wrote on last edited by
        #3

        Hi, John. Thanks for your answer. A typesafe cast will not throw the exception, that's right. But nevertheless my designer will not work. And: the type is correct. I even used the full qualified class name to be sure. When I log a message printing the typename it's ok, too. Very strange behaviour... Have you (or somebody else) written a designer which works well? Regards Jürgen

        J 1 Reply Last reply
        0
        • J Jurgen Muller jmse

          Hi, John. Thanks for your answer. A typesafe cast will not throw the exception, that's right. But nevertheless my designer will not work. And: the type is correct. I even used the full qualified class name to be sure. When I log a message printing the typename it's ok, too. Very strange behaviour... Have you (or somebody else) written a designer which works well? Regards Jürgen

          J Offline
          J Offline
          John Petersen
          wrote on last edited by
          #4

          Hello again I think i know what is wrong. The ContainerControlDesigner is used to create a designer that can contain several controls. Maybe you should derive from CompositeControlDesigner instead? i found a sample of this here Kind Regards, John Petersen

          J 1 Reply Last reply
          0
          • J John Petersen

            Hello again I think i know what is wrong. The ContainerControlDesigner is used to create a designer that can contain several controls. Maybe you should derive from CompositeControlDesigner instead? i found a sample of this here Kind Regards, John Petersen

            J Offline
            J Offline
            Jurgen Muller jmse
            wrote on last edited by
            #5

            Hi, John. Thank you for the link. I've found this already and unfortunately it didn't help me. I think the ContainerControlDesigner is the right pick. VS help says: "(ContainerControlDesigner) provides designer functionality for controls that contain child controls or properties that can be modified at design time." Controls placed on MyComponent are designed by their own designer... Regards Jürgen

            1 Reply Last reply
            0
            • J Jurgen Muller jmse

              Hi, all. I'm having a strange problem while writing custom server control. The control works fine so far, but I wanted to add some features which support the developer while using the control in the Visual Studio designer. Therefore I've added a designer which extends ContainerControlDesigner... Code fragment: public class MyDesigner : ContainerControlDesigner { public override string FrameCaption { get { MyComponent c = (MyComponent)base.Component; return "MyComponent - " + c.MyProperty; } } Problem: Sometimes the cast MyComponent c = (MyComponent)base.Component will throw an exception - and sometimes not. I haven't found a reason for this. The type of base.Component is MyComponent. I've checked this in the debugger. Even Visual Studio shows me all properties of this type during debugging. I've modified the code and read the value of c.MyProperty via reflection - and it works... Any ideas what might be wrong or what happens here? :confused: Any help appreciated. Regards Jürgen Stuttgart Germany

              M Offline
              M Offline
              minhpc_bk
              wrote on last edited by
              #6

              Hi there, + What exception do you have? + Like John said, are you sure that you choose the right base class for your custom designer? Also what does your MyComponent look like? + And you may want to verify the type of the component in the method below:

              public override void Initialize(IComponent component)
              {
              //Your code to verify if the component is of the expected type.

                base.Initialize(component);
              

              }

              J 1 Reply Last reply
              0
              • M minhpc_bk

                Hi there, + What exception do you have? + Like John said, are you sure that you choose the right base class for your custom designer? Also what does your MyComponent look like? + And you may want to verify the type of the component in the method below:

                public override void Initialize(IComponent component)
                {
                //Your code to verify if the component is of the expected type.

                  base.Initialize(component);
                

                }

                J Offline
                J Offline
                Jurgen Muller jmse
                wrote on last edited by
                #7

                Hi, I've tried what you mentioned: public override void Initialize(IComponent component) { base.Initialize(component); try { MyComponent o = (MyComponent)component; } catch (Exception ex) { string s = ex.Message; } } - Exception: Unable to cast object of type 'jmse.WebControls.MyComponent' to type 'jmse.WebControls.MyComponent'. - InnerException is null - the declaration of MyComponent: public class MyComponent : View, INamingContainer - I've modified the source but still have the same effect; declaration of the designer: public class MyComponentDesigner : ViewDesigner - MyComponent is placed on a MultiView

                M 1 Reply Last reply
                0
                • J Jurgen Muller jmse

                  Hi, I've tried what you mentioned: public override void Initialize(IComponent component) { base.Initialize(component); try { MyComponent o = (MyComponent)component; } catch (Exception ex) { string s = ex.Message; } } - Exception: Unable to cast object of type 'jmse.WebControls.MyComponent' to type 'jmse.WebControls.MyComponent'. - InnerException is null - the declaration of MyComponent: public class MyComponent : View, INamingContainer - I've modified the source but still have the same effect; declaration of the designer: public class MyComponentDesigner : ViewDesigner - MyComponent is placed on a MultiView

                  M Offline
                  M Offline
                  minhpc_bk
                  wrote on last edited by
                  #8

                  Below is a quick and simple control that is working in my case:

                  [ToolboxData("<{0}:MyComponent runat=\"server\"></{0}:MyComponent>"), Designer(typeof(MyDesigner)), ParseChildren(false)]
                  public class MyComponent : View, INamingContainer
                  {
                  public MyComponent()
                  {
                  }

                  private string myVar;
                                  
                  public string MyProperty
                  {
                      get { return myVar; }
                      set { myVar = value; }
                  }
                  

                  }

                  public class MyDesigner : ViewDesigner
                  {
                  string error = string.Empty;

                  public override void Initialize(IComponent component)
                  {
                      base.Initialize(component);
                               
                      try
                      {
                          MyComponent o = (MyComponent)component;
                      }
                      catch (Exception ex)
                      {
                          error = ex.Message;
                      }
                  }
                                   
                  public override string FrameCaption
                  {
                      get
                      {
                          if (!string.IsNullOrEmpty(error))
                              return error;
                                   
                          MyComponent c = (MyComponent)base.Component;
                          return "MyComponent - " + c.MyProperty;
                      }
                  }
                  

                  }

                  A snippet of the ASP.NET markup of the testing web page:

                  <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="1">
                  <asp:View ID="View1" runat="server">
                  <asp:TextBox ID="TextBox1" runat="server" Text="dsd"></asp:TextBox></asp:View>
                  <cc1:MyComponent ID="MyComponent1" runat="server" MyProperty="My Custom View">
                  <asp:Button id="Button1" runat="server" Text="Pushed Me!" />
                  </cc1:MyComponent>
                  </asp:MultiView>

                  Btw, do you put your component control and its designer in a seperate web control library project or in the same web site project of the application?

                  J 1 Reply Last reply
                  0
                  • M minhpc_bk

                    Below is a quick and simple control that is working in my case:

                    [ToolboxData("<{0}:MyComponent runat=\"server\"></{0}:MyComponent>"), Designer(typeof(MyDesigner)), ParseChildren(false)]
                    public class MyComponent : View, INamingContainer
                    {
                    public MyComponent()
                    {
                    }

                    private string myVar;
                                    
                    public string MyProperty
                    {
                        get { return myVar; }
                        set { myVar = value; }
                    }
                    

                    }

                    public class MyDesigner : ViewDesigner
                    {
                    string error = string.Empty;

                    public override void Initialize(IComponent component)
                    {
                        base.Initialize(component);
                                 
                        try
                        {
                            MyComponent o = (MyComponent)component;
                        }
                        catch (Exception ex)
                        {
                            error = ex.Message;
                        }
                    }
                                     
                    public override string FrameCaption
                    {
                        get
                        {
                            if (!string.IsNullOrEmpty(error))
                                return error;
                                     
                            MyComponent c = (MyComponent)base.Component;
                            return "MyComponent - " + c.MyProperty;
                        }
                    }
                    

                    }

                    A snippet of the ASP.NET markup of the testing web page:

                    <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="1">
                    <asp:View ID="View1" runat="server">
                    <asp:TextBox ID="TextBox1" runat="server" Text="dsd"></asp:TextBox></asp:View>
                    <cc1:MyComponent ID="MyComponent1" runat="server" MyProperty="My Custom View">
                    <asp:Button id="Button1" runat="server" Text="Pushed Me!" />
                    </cc1:MyComponent>
                    </asp:MultiView>

                    Btw, do you put your component control and its designer in a seperate web control library project or in the same web site project of the application?

                    J Offline
                    J Offline
                    Jurgen Muller jmse
                    wrote on last edited by
                    #9

                    Hi, thanks again for your help. I've tried your code and the component works fine. No errors; all casts are OK. I've added my component to the same test project and again the error occurs. What I found out: the error only occurs when the component is added from the toolbox to a WebForm (regardless of added by doubleclick or dragged with the mouse). When the WebForm is closed and opened again in the designer, all of my components are processed without error. My component and the designer are in an extra WebLibraray project. For debugging I use two instances of VS 2005. The complete declaration of my component: [ AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), ToolboxData("<{0}:MyComponent runat=\"server\"> "), ToolboxBitmap(typeof(MyComponent), "MyComponent.bmp"), Designer(typeof(MyComponentDesigner)), ParseChildren(false), PersistChildren(true) ] public class MyComponent: View, INamingContainer Any idaes? Regards Jürgen -- modified at 4:25 Thursday 31st August, 2006

                    M 1 Reply Last reply
                    0
                    • J Jurgen Muller jmse

                      Hi, thanks again for your help. I've tried your code and the component works fine. No errors; all casts are OK. I've added my component to the same test project and again the error occurs. What I found out: the error only occurs when the component is added from the toolbox to a WebForm (regardless of added by doubleclick or dragged with the mouse). When the WebForm is closed and opened again in the designer, all of my components are processed without error. My component and the designer are in an extra WebLibraray project. For debugging I use two instances of VS 2005. The complete declaration of my component: [ AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), ToolboxData("<{0}:MyComponent runat=\"server\"> "), ToolboxBitmap(typeof(MyComponent), "MyComponent.bmp"), Designer(typeof(MyComponentDesigner)), ParseChildren(false), PersistChildren(true) ] public class MyComponent: View, INamingContainer Any idaes? Regards Jürgen -- modified at 4:25 Thursday 31st August, 2006

                      M Offline
                      M Offline
                      minhpc_bk
                      wrote on last edited by
                      #10

                      Hi there, + Do you get the error right after you add the control onto the web page or after you do something and the error occurs? + If you manually add the custom component in the source view and switch back to the Design view, do you still get the error? + Do you plan to use the MyComponent with the MultiView control?

                      J 1 Reply Last reply
                      0
                      • M minhpc_bk

                        Hi there, + Do you get the error right after you add the control onto the web page or after you do something and the error occurs? + If you manually add the custom component in the source view and switch back to the Design view, do you still get the error? + Do you plan to use the MyComponent with the MultiView control?

                        J Offline
                        J Offline
                        Jurgen Muller jmse
                        wrote on last edited by
                        #11

                        Hi,

                        minhpc_bk wrote:

                        + Do you get the error right after you add the control onto the web page or after you do something and the error occurs?

                        the error occurs right after adding the control

                        minhpc_bk wrote:

                        + If you manually add the custom component in the source view and switch back to the Design view, do you still get the error?

                        as I wrote, if I open the WebForm with designer all MyComponent controls on that WebForm are displayed without error; so adding controls in the source is OK

                        minhpc_bk wrote:

                        + Do you plan to use the MyComponent with the MultiView control?

                        yes, MyComponent is used in a MutliView

                        M 1 Reply Last reply
                        0
                        • J Jurgen Muller jmse

                          Hi,

                          minhpc_bk wrote:

                          + Do you get the error right after you add the control onto the web page or after you do something and the error occurs?

                          the error occurs right after adding the control

                          minhpc_bk wrote:

                          + If you manually add the custom component in the source view and switch back to the Design view, do you still get the error?

                          as I wrote, if I open the WebForm with designer all MyComponent controls on that WebForm are displayed without error; so adding controls in the source is OK

                          minhpc_bk wrote:

                          + Do you plan to use the MyComponent with the MultiView control?

                          yes, MyComponent is used in a MutliView

                          M Offline
                          M Offline
                          minhpc_bk
                          wrote on last edited by
                          #12

                          Can you reset the Toolbox of VS and see if you keep getting the error? Also, if you still have the error, can you try to debug to see if the VS designer loads the right version of the component assembly as the error type casting of the same type leads me think the designer loads two versions of the assembly.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups