Web Custom/Composite controls default property render issue.

  • Thread starter Andrei Sinelnikov
  • Start date
A

Andrei Sinelnikov

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
 
V

Victor Garcia Aprea [MVP]

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 said:
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
 
A

Andrei Sinelnikov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top