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. Custome Control Problem

Custome Control Problem

Scheduled Pinned Locked Moved ASP.NET
tutorialsysadminhelp
13 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.
  • M Offline
    M Offline
    Muhammad Gouda
    wrote on last edited by
    #1

    I am creating a Custom Server Control, where one of the properties should be the data type of another property for example: for an instance x of my control, I need the user of my control to be able to write something like:

    x.property1 = "abc";
    x.property1Type = System.String;

    So, how to define property1Type within the class

    foreach(Minute m in MyLife) myExperience++;

    A N M 3 Replies Last reply
    0
    • M Muhammad Gouda

      I am creating a Custom Server Control, where one of the properties should be the data type of another property for example: for an instance x of my control, I need the user of my control to be able to write something like:

      x.property1 = "abc";
      x.property1Type = System.String;

      So, how to define property1Type within the class

      foreach(Minute m in MyLife) myExperience++;

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      Best way to do it using Generic in C#

      cheers, Abhijit My Latest Article : IIS 7.0 and Deploying Asp.Net WebSites on IIS 7.0

      M 1 Reply Last reply
      0
      • A Abhijit Jana

        Best way to do it using Generic in C#

        cheers, Abhijit My Latest Article : IIS 7.0 and Deploying Asp.Net WebSites on IIS 7.0

        M Offline
        M Offline
        Muhammad Gouda
        wrote on last edited by
        #3

        I am familiar with Generic BUT, I can not create a custome control from a generic class (I think)

        foreach(Minute m in MyLife) myExperience++;

        A 1 Reply Last reply
        0
        • M Muhammad Gouda

          I am familiar with Generic BUT, I can not create a custome control from a generic class (I think)

          foreach(Minute m in MyLife) myExperience++;

          A Offline
          A Offline
          Abhijit Jana
          wrote on last edited by
          #4

          Mohammed Gouda wrote:

          BUT, I can not create a custome control from a generic class (I think)

          why ? you can create separate class and create object of that class and pass the value and data type.

          cheers, Abhijit My Latest Article : IIS 7.0 and Deploying Asp.Net WebSites on IIS 7.0

          N 1 Reply Last reply
          0
          • M Muhammad Gouda

            I am creating a Custom Server Control, where one of the properties should be the data type of another property for example: for an instance x of my control, I need the user of my control to be able to write something like:

            x.property1 = "abc";
            x.property1Type = System.String;

            So, how to define property1Type within the class

            foreach(Minute m in MyLife) myExperience++;

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            AFAIK, you can't use generics here as you won't be able to view this control in the designer if you have a generic type with your class declaration. But you could do something like this

            class YourClass
            {
            public Type Property1Type { get; set; }

                object property1;
                public object Property1 
                {
                    get { return property1; }
                    set
                    {
                        if (value.GetType() == this.Property1Type)
                            property1 = value;
                        else
                            throw new Exception("Invalid type");
                    }
                }
            }
            

            YourClass a = new YourClass();
            a.Property1Type = typeof(System.String);
            a.Property1 = "Hello";

            Above code allows to set only the specified type to "Property1". I am not sure this is the correct approach though. If you could tell me why you need this, there may be better ways to do this. If you don't want designer support for this control you can use generic parameter with the class declaration. Then the code will be pretty nice.

            class YourClassInGenerics<Property1Type>
            {
            public Property1Type Property1 { get; set; }
            }

            YourClassInGenerics<string> yourClass = new YourClassInGenerics<string>();
            yourClass.Property1 = "Hello";

            All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

            A M 2 Replies Last reply
            0
            • A Abhijit Jana

              Mohammed Gouda wrote:

              BUT, I can not create a custome control from a generic class (I think)

              why ? you can create separate class and create object of that class and pass the value and data type.

              cheers, Abhijit My Latest Article : IIS 7.0 and Deploying Asp.Net WebSites on IIS 7.0

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              Abhijit Jana wrote:

              why ?

              Creating generic control classes won't get designer support as designer don't know which type to be specified. If there is no designer support required, I believe it is possible.

              All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

              1 Reply Last reply
              0
              • N N a v a n e e t h

                AFAIK, you can't use generics here as you won't be able to view this control in the designer if you have a generic type with your class declaration. But you could do something like this

                class YourClass
                {
                public Type Property1Type { get; set; }

                    object property1;
                    public object Property1 
                    {
                        get { return property1; }
                        set
                        {
                            if (value.GetType() == this.Property1Type)
                                property1 = value;
                            else
                                throw new Exception("Invalid type");
                        }
                    }
                }
                

                YourClass a = new YourClass();
                a.Property1Type = typeof(System.String);
                a.Property1 = "Hello";

                Above code allows to set only the specified type to "Property1". I am not sure this is the correct approach though. If you could tell me why you need this, there may be better ways to do this. If you don't want designer support for this control you can use generic parameter with the class declaration. Then the code will be pretty nice.

                class YourClassInGenerics<Property1Type>
                {
                public Property1Type Property1 { get; set; }
                }

                YourClassInGenerics<string> yourClass = new YourClassInGenerics<string>();
                yourClass.Property1 = "Hello";

                All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                A Offline
                A Offline
                Abhijit Jana
                wrote on last edited by
                #7

                Thanks for Correcting Me !!!

                cheers, Abhijit My Latest Article : IIS 7.0 and Deploying Asp.Net WebSites on IIS 7.0

                N 1 Reply Last reply
                0
                • A Abhijit Jana

                  Thanks for Correcting Me !!!

                  cheers, Abhijit My Latest Article : IIS 7.0 and Deploying Asp.Net WebSites on IIS 7.0

                  N Offline
                  N Offline
                  N a v a n e e t h
                  wrote on last edited by
                  #8

                  You are welcome :)

                  All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                  A 1 Reply Last reply
                  0
                  • N N a v a n e e t h

                    You are welcome :)

                    All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                    A Offline
                    A Offline
                    Abhijit Jana
                    wrote on last edited by
                    #9

                    did you check my recent article ?

                    cheers, Abhijit My Latest Article : IIS 7.0 and Deploying Asp.Net WebSites on IIS 7.0

                    1 Reply Last reply
                    0
                    • M Muhammad Gouda

                      I am creating a Custom Server Control, where one of the properties should be the data type of another property for example: for an instance x of my control, I need the user of my control to be able to write something like:

                      x.property1 = "abc";
                      x.property1Type = System.String;

                      So, how to define property1Type within the class

                      foreach(Minute m in MyLife) myExperience++;

                      M Offline
                      M Offline
                      Muhammad Gouda
                      wrote on last edited by
                      #10

                      Thanks to all participants I got a safe solution Instead of Generic or unknown datatype. I created an abstract class from which I inherited all other classes of which objects may be bound to my new datasource It works well :-\

                      foreach(Minute m in MyLife) myExperience++;

                      1 Reply Last reply
                      0
                      • N N a v a n e e t h

                        AFAIK, you can't use generics here as you won't be able to view this control in the designer if you have a generic type with your class declaration. But you could do something like this

                        class YourClass
                        {
                        public Type Property1Type { get; set; }

                            object property1;
                            public object Property1 
                            {
                                get { return property1; }
                                set
                                {
                                    if (value.GetType() == this.Property1Type)
                                        property1 = value;
                                    else
                                        throw new Exception("Invalid type");
                                }
                            }
                        }
                        

                        YourClass a = new YourClass();
                        a.Property1Type = typeof(System.String);
                        a.Property1 = "Hello";

                        Above code allows to set only the specified type to "Property1". I am not sure this is the correct approach though. If you could tell me why you need this, there may be better ways to do this. If you don't want designer support for this control you can use generic parameter with the class declaration. Then the code will be pretty nice.

                        class YourClassInGenerics<Property1Type>
                        {
                        public Property1Type Property1 { get; set; }
                        }

                        YourClassInGenerics<string> yourClass = new YourClassInGenerics<string>();
                        yourClass.Property1 = "Hello";

                        All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                        M Offline
                        M Offline
                        Muhammad Gouda
                        wrote on last edited by
                        #11

                        Thanks for good effort

                        N a v a n e e t h wrote:

                        If you could tell me why you need this, there may be better ways to do this.

                        Exactly, I want to bind business objects to TreeView which does not support this sort of binding. So, I created my own datasource which is supposed to support a variety of business objects I got a safe solution Instead of Generic or unknown datatype. I created an abstract class from which I inherited all other classes of which objects may be bound to my new datasource It works well

                        N a v a n e e t h wrote:

                        If you don't want designer support for this control you can use generic parameter with the class declaration. Then the code will be pretty nice.

                        Yes, I need a designer support However, Thanks N a v a n e e t h for your great help. You are one of the best of CP :-D

                        foreach(Minute m in MyLife) myExperience++;

                        N 1 Reply Last reply
                        0
                        • M Muhammad Gouda

                          Thanks for good effort

                          N a v a n e e t h wrote:

                          If you could tell me why you need this, there may be better ways to do this.

                          Exactly, I want to bind business objects to TreeView which does not support this sort of binding. So, I created my own datasource which is supposed to support a variety of business objects I got a safe solution Instead of Generic or unknown datatype. I created an abstract class from which I inherited all other classes of which objects may be bound to my new datasource It works well

                          N a v a n e e t h wrote:

                          If you don't want designer support for this control you can use generic parameter with the class declaration. Then the code will be pretty nice.

                          Yes, I need a designer support However, Thanks N a v a n e e t h for your great help. You are one of the best of CP :-D

                          foreach(Minute m in MyLife) myExperience++;

                          N Offline
                          N Offline
                          N a v a n e e t h
                          wrote on last edited by
                          #12

                          Mohammed Gouda wrote:

                          I created an abstract class from which I inherited all other classes of which objects may be bound to my new datasource

                          Good. Is this abstract class has some default functionality ? or it is just acting as a base class ? If it is acting as a base class, you should consider moving to an interface. Controls like GridView/Repeater supports data binding objects which has IEnumerable implemented. Your control also could do something similar.

                          Mohammed Gouda wrote:

                          You are one of the best of CP

                          Thanks. I am delighted :)

                          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                          M 1 Reply Last reply
                          0
                          • N N a v a n e e t h

                            Mohammed Gouda wrote:

                            I created an abstract class from which I inherited all other classes of which objects may be bound to my new datasource

                            Good. Is this abstract class has some default functionality ? or it is just acting as a base class ? If it is acting as a base class, you should consider moving to an interface. Controls like GridView/Repeater supports data binding objects which has IEnumerable implemented. Your control also could do something similar.

                            Mohammed Gouda wrote:

                            You are one of the best of CP

                            Thanks. I am delighted :)

                            All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                            M Offline
                            M Offline
                            Muhammad Gouda
                            wrote on last edited by
                            #13

                            N a v a n e e t h wrote:

                            Controls like GridView/Repeater supports data binding objects which has IEnumerable implemented. Your control also could do something similar.

                            Yes, My class alreay implement IHierarchicalEnumerable cause I deal with TreeView control. Thanks for the hint

                            foreach(Minute m in MyLife) myExperience++;

                            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