Web Custom/Composite controls default property render issue.

Discussion in 'ASP .Net Building Controls' started by Andrei Sinelnikov, May 7, 2004.

  1. 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").Attributes
    Dim myAttribute As DefaultValueAttribute =
    CType(attributes(GetType(DefaultValueAttribute)),
    DefaultValueAttribute)

    MyBase.CssClass = [Enum].Parse(GetType(CssType),
    myAttribute.Value)
    End If
    Return [Enum].Parse(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.TextBoxWithValidationDesigner"),
    _
    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.TextBoxWithValidationDesigner"),
    _
    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, May 7, 2004
    #1
    1. Advertising

  2. 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 parser, 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 parser 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:
    http://obies.com/vga/blog.aspx
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

    "Andrei Sinelnikov" <> wrote in message
    news:...
    > 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").Attributes
    > Dim myAttribute As DefaultValueAttribute =
    > CType(attributes(GetType(DefaultValueAttribute)),
    > DefaultValueAttribute)
    >
    > MyBase.CssClass = [Enum].Parse(GetType(CssType),
    > myAttribute.Value)
    > End If
    > Return [Enum].Parse(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.TextBoxWithValidationDesigner"),
    > _
    > 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.TextBoxWithValidationDesigner"),
    > _
    > 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], May 8, 2004
    #2
    1. Advertising

  3. 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.TextBoxWithValidationDesigner"),
    _
    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").Attributes
    Dim myAttribute As DefaultValueAttribute =
    CType(attributes(GetType(DefaultValueAttribute)),
    DefaultValueAttribute)

    Me.CssClass = [Enum].Parse(GetType(CssType),
    myAttribute.Value)
    End If
    Return [Enum].Parse(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, May 10, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    749
  2. sleigh
    Replies:
    1
    Views:
    2,741
    sleigh
    Feb 12, 2004
  3. rodchar
    Replies:
    0
    Views:
    410
    rodchar
    Nov 26, 2007
  4. Replies:
    1
    Views:
    408
    Teemu Keiski
    Jul 2, 2003
  5. Chad
    Replies:
    0
    Views:
    255
Loading...

Share This Page