Professional Web Applications Themes

Web Custom/Composite controls default property render issue. - ASP.NET Building Controls

Hi, I'm trying to build TextBox based custom web control. Part of this control is shadowed CssClass property: <Description("Choose Css property from the list.") _ , DefaultValue(GetType(CssType), "RequiredInput") _ , TypeConverter(GetType(EnumConverter))> _ Public Shadows Property CssClass() As CssType Get If MyBase.CssClass = "" Then ' If MyBase.CssClass empty - retrieve default value for this property ' and assign it to the MyBase.CssClass. Dim attributes As AttributeCollection = TypeDescriptor.GetProperties(Me)("CssClass").Attri butes Dim myAttribute As DefaultValueAttribute = CType(attributes(GetType(DefaultValueAttribute)), DefaultValueAttribute) MyBase.CssClass = [Enum].P(GetType(CssType), myAttribute.Value) End If Return [Enum].P(GetType(CssType), MyBase.CssClass) End Get Set(ByVal Value As CssType) MyBase.CssClass = [Enum].GetName(GetType(CssType), Value) End Set End Property Enum ...

  1. #1

    Default Web Custom/Composite controls default property render issue.

    Hi,

    I'm trying to build TextBox based custom web control. Part of this
    control is shadowed CssClass property:

    <Description("Choose Css property from the list.") _
    , DefaultValue(GetType(CssType), "RequiredInput") _
    , TypeConverter(GetType(EnumConverter))> _
    Public Shadows Property CssClass() As CssType
    Get
    If MyBase.CssClass = "" Then
    ' If MyBase.CssClass empty - retrieve default value
    for this property
    ' and assign it to the MyBase.CssClass.
    Dim attributes As AttributeCollection =
    TypeDescriptor.GetProperties(Me)("CssClass").Attri butes
    Dim myAttribute As DefaultValueAttribute =
    CType(attributes(GetType(DefaultValueAttribute)),
    DefaultValueAttribute)

    MyBase.CssClass = [Enum].P(GetType(CssType),
    myAttribute.Value)
    End If
    Return [Enum].P(GetType(CssType), MyBase.CssClass)
    End Get
    Set(ByVal Value As CssType)
    MyBase.CssClass = [Enum].GetName(GetType(CssType), Value)
    End Set
    End Property


    Enum CssType As Integer
    UnDefined
    Input
    RequiredInput
    DisabledInput
    DependentInput
    RequiredInputNoBorder
    Data
    End Enum


    I know that I can add any design-time Custom Control attributes to the
    the ToolboxData attribute in the class declaration area (in the
    following example you can see CssClass='DisabledInput'):

    <DefaultProperty("Text"), _
    Designer("Solution.WebControls.Design.TextBoxWithV alidationDesigner"),
    _
    ToolboxData("<{0}:TextBoxWithValidation
    id='utxtTextBoxWithValidation' runat='server'
    CssClass='DisabledInput'></{0}:TextBoxWithValidation>")> _
    Public Class TextBoxWithValidation
    Inherits System.Web.UI.WebControls.TextBox
    '........

    End Class


    QUESTION:

    How can I force control to render some (or even all) properties with
    their predefined default values?
    In other words: Let's say that I have 2 properties: CssClass and Text.
    Default Value for the CssClass - DefaultValue(GetType(CssType),
    "RequiredInput")
    Default Value for the Text - DefaultValue("Type here...")
    I want to leave class declaration ToolboxData attribute
    "properties-free":

    <DefaultProperty("Text"), _
    Designer("Solution.WebControls.Design.TextBoxWithV alidationDesigner"),
    _
    ToolboxData("<{0}:TextBoxWithValidation
    id='utxtTextBoxWithValidation'
    runat='server'></{0}:TextBoxWithValidation>")> _
    Public Class TextBoxWithValidation
    Inherits System.Web.UI.WebControls.TextBox
    '........
    End Class

    But as soon as I drug&drop control from the toolbox to the page I want
    to see in HTML following code:

    <AS:TextBoxWithValidation id="utxtTextBoxWithValidation"
    runat="server" CssClass="RequiredInput" Text="Type
    here..."></AS:TextBoxWithValidation>

    Any ideas?


    Thank you in regards,
    Andrei Sinelnikov
    Andrei Sinelnikov Guest

  2. #2

    Default Re: Web Custom/Composite controls default property render issue.

    Hi Andrei,

    The DefaultValueAttribute is no meant to initialize a property value to some
    value, its only meant to be used by designers like VS.NET to properly reset
    values, to decide when to serialize a member value, etc. You need to
    initialize a property value explicity in code then use that same value as
    the argument for the DefaultValueAttribute.

    Also what you're trying to do about always persisting default values to the
    aspx is exactly the inverse of how things were designed to work :-) Let me
    explain a bit what I mean by this:

    All of the properties specified at design-time will end up converted to code
    by ASP.NET page pr, this happens on the fly and its totally transparent,
    so something like this:

    <cc:MyControl Mask="blah" />

    will get translated to some code like the following:
    [C#]
    MyControl ctrl = new MyControl ();
    ctrl.Mask = "blah";

    Now if "blah" is the default value for property Mask then there is no reason
    for the ASP.NET pr to generate code for it as you will be already
    setting it to "blah" in your control code. Adding the unnecessary attribute
    will only produce larger and slower assemblies.

    Let me know if this is clear to you,


    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    [url]http://obies.com/vga/blog.aspx[/url]
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

    "Andrei Sinelnikov" <andreisinelnikovsierrasystems.com> wrote in message
    news:29404afc.0405071306.33408500posting.google.c om...
    > Hi,
    >
    > I'm trying to build TextBox based custom web control. Part of this
    > control is shadowed CssClass property:
    >
    > <Description("Choose Css property from the list.") _
    > , DefaultValue(GetType(CssType), "RequiredInput") _
    > , TypeConverter(GetType(EnumConverter))> _
    > Public Shadows Property CssClass() As CssType
    > Get
    > If MyBase.CssClass = "" Then
    > ' If MyBase.CssClass empty - retrieve default value
    > for this property
    > ' and assign it to the MyBase.CssClass.
    > Dim attributes As AttributeCollection =
    > TypeDescriptor.GetProperties(Me)("CssClass").Attri butes
    > Dim myAttribute As DefaultValueAttribute =
    > CType(attributes(GetType(DefaultValueAttribute)),
    > DefaultValueAttribute)
    >
    > MyBase.CssClass = [Enum].P(GetType(CssType),
    > myAttribute.Value)
    > End If
    > Return [Enum].P(GetType(CssType), MyBase.CssClass)
    > End Get
    > Set(ByVal Value As CssType)
    > MyBase.CssClass = [Enum].GetName(GetType(CssType), Value)
    > End Set
    > End Property
    >
    >
    > Enum CssType As Integer
    > UnDefined
    > Input
    > RequiredInput
    > DisabledInput
    > DependentInput
    > RequiredInputNoBorder
    > Data
    > End Enum
    >
    >
    > I know that I can add any design-time Custom Control attributes to the
    > the ToolboxData attribute in the class declaration area (in the
    > following example you can see CssClass='DisabledInput'):
    >
    > <DefaultProperty("Text"), _
    > Designer("Solution.WebControls.Design.TextBoxWithV alidationDesigner"),
    > _
    > ToolboxData("<{0}:TextBoxWithValidation
    > id='utxtTextBoxWithValidation' runat='server'
    > CssClass='DisabledInput'></{0}:TextBoxWithValidation>")> _
    > Public Class TextBoxWithValidation
    > Inherits System.Web.UI.WebControls.TextBox
    > '........
    >
    > End Class
    >
    >
    > QUESTION:
    >
    > How can I force control to render some (or even all) properties with
    > their predefined default values?
    > In other words: Let's say that I have 2 properties: CssClass and Text.
    > Default Value for the CssClass - DefaultValue(GetType(CssType),
    > "RequiredInput")
    > Default Value for the Text - DefaultValue("Type here...")
    > I want to leave class declaration ToolboxData attribute
    > "properties-free":
    >
    > <DefaultProperty("Text"), _
    > Designer("Solution.WebControls.Design.TextBoxWithV alidationDesigner"),
    > _
    > ToolboxData("<{0}:TextBoxWithValidation
    > id='utxtTextBoxWithValidation'
    > runat='server'></{0}:TextBoxWithValidation>")> _
    > Public Class TextBoxWithValidation
    > Inherits System.Web.UI.WebControls.TextBox
    > '........
    > End Class
    >
    > But as soon as I drug&drop control from the toolbox to the page I want
    > to see in HTML following code:
    >
    > <AS:TextBoxWithValidation id="utxtTextBoxWithValidation"
    > runat="server" CssClass="RequiredInput" Text="Type
    > here..."></AS:TextBoxWithValidation>
    >
    > Any ideas?
    >
    >
    > Thank you in regards,
    > Andrei Sinelnikov

    Victor Garcia Aprea [MVP] Guest

  3. #3

    Default Re: Web Custom/Composite controls default property render issue.

    Thank you, Victor.

    Now I get AmbiguousMatchException... :-) (As long as
    CssClass='Something' appears in HTML)

    I think that I'm doing something wrong with shadowing CssClass...
    There are lack of information about shadowing base class properties.
    Can I do it like that?

    Imports System.ComponentModel
    Imports System.Web.UI.WebControls

    <DefaultProperty("Text"), _
    Designer("Solution.WebControls.Design.TextBoxWithV alidationDesigner"),
    _
    ToolboxData("<{0}:TextBoxWithValidation
    id='utxtTextBoxWithValidation'
    runat='server'></{0}:TextBoxWithValidation>")> _
    Public Class TextBoxWithValidation
    Inherits System.Web.UI.WebControls.TextBox

    #Region " Properties "
    <DefaultValue(GetType(CssType), "Input") _
    , TypeConverter(GetType(EnumConverter))> _
    Public Shadows Property CssClass() As CssType
    Get
    If MyBase.CssClass = "" Then
    ' If MyBase.CssClass empty - retrieve default value
    for this property
    ' and assign it to the MyBase.CssClass.
    Dim attributes As AttributeCollection =
    TypeDescriptor.GetProperties(Me)("CssClass").Attri butes
    Dim myAttribute As DefaultValueAttribute =
    CType(attributes(GetType(DefaultValueAttribute)),
    DefaultValueAttribute)

    Me.CssClass = [Enum].P(GetType(CssType),
    myAttribute.Value)
    End If
    Return [Enum].P(GetType(CssType), MyBase.CssClass)
    End Get
    Set(ByVal Value As CssType)
    MyBase.CssClass = [Enum].GetName(GetType(CssType), Value)
    End Set
    End Property
    #End Region

    End Class


    Thank you,

    Andrei Sinelnikov
    Andrei Sinelnikov Guest

Similar Threads

  1. Property color not found on mx.controls.Text and there is no default value.
    By Satara@gmail.com in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: June 29th, 09:51 PM
  2. Need help with composite custom controls ...
    By MEHRAN SEDAGHATPOUR in forum ASP.NET Building Controls
    Replies: 0
    Last Post: February 14th, 09:19 PM
  3. Composite controls and Property Accessors
    By Sam Samnah in forum ASP.NET Building Controls
    Replies: 0
    Last Post: June 27th, 05:31 PM
  4. Composite control with dynamic controls depending on a property value
    By Matt Sollars in forum ASP.NET Building Controls
    Replies: 7
    Last Post: July 30th, 12:49 PM
  5. Composite custom controls events
    By apple in forum ASP.NET Building Controls
    Replies: 9
    Last Post: February 9th, 08:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139