Init Control when dropped on form??

A

A Traveler

Hello all,

I have a control i wrote which inherits from asp:label. It worked fine. It
extends asp:label with a "For" property which implements the "for" attribute
of an html label tag (yes i know the latest framework vers. added this, but
i want ot get mine right as a learning exp.).

I added a designer class which turns the For property into a dropdown list
of the names of all the controls so you can simply pick one. When i added
the designer, the control no longer properly initialized its Text property
when you drop it on a webform. I THINK the problem is in then designer's
Initialize sub.

The code for my designer is shown below.

When i drop the control on a page now, the text property starts out as ""
instead of with the controls id. If someone could tell me how to get this to
properly initialize.

Thanks in advance,
- Arthur Dent.

===== BEGIN CODE ================================

Public Class LabelDesigner
Inherits ControlDesigner

Private pLabel As Label

Public Overrides Sub Initialize(ByVal component As IComponent)
MyBase.Initialize(component)
pLabel = CType(component, Label)
'pLabel.Text = pLabel.ID
End Sub

Public Property [For]() As String
Get
Return pLabel.For
End Get
Set(ByVal Value As String)
pLabel.For = Value
End Set
End Property

Protected Overrides Sub PreFilterProperties(ByVal properties As
IDictionary)
MyBase.PostFilterProperties(properties)
Dim prop As PropertyDescriptor = CType(properties("For"),
PropertyDescriptor)
If Not prop Is Nothing Then
prop = TypeDescriptor.CreateProperty(Me.GetType(), prop, New
Attribute() {New TypeConverterAttribute(GetType(ForConvertor))})
properties("For") = prop
End If
End Sub
End Class
 
W

WALDO

But first let me ask you this.

Why create a ControlDesigner just to implement a TypeConverter on a single property?

Why not this? (I'm presuming your label class is called PLabel)
[VB.Net]
Public Class PLabel
Inherits System.Web.UI.WebControls.Label

Private _for As String

<TypeConverterAttribute(GetType(ForConvertor))> _
Public Property [For] As String
Get
Return Me._for
End Get
Set(Value As String)
Me._for = Value
End Set
End Property

Public Overrides Sub OnInit()
Me._for = Me.Id
End Sub
End Class
 
W

WALDO

I forgot something

Public Overrides Sub OnInit()
MyBase.OnInit() 'Forgot this line
Me._for = Me.Id
End Sub
 
A

A Traveler

Well, i had gotten the code from someone else who was doing the same thing,
providing the dropdown list of controls on the form. I dont know a whole lot
about all the designer and type convertor stuff.... still very new to any
advanced control design.

Thanks for the help, removing the designer did the trick. Now the base label
does properly initialize itself. However, you cant use the OnInit override
to set initial properties. Then when you close the designer and reopen it,
it reinits them and overrides and changes you might have made.

Thanks again though for your help clearing up the labels Text property.

- Arthur Dent


WALDO said:
But first let me ask you this.

Why create a ControlDesigner just to implement a TypeConverter on a single
property?

Why not this? (I'm presuming your label class is called PLabel)
[VB.Net]
Public Class PLabel
Inherits System.Web.UI.WebControls.Label

Private _for As String

<TypeConverterAttribute(GetType(ForConvertor))> _
Public Property [For] As String
Get
Return Me._for
End Get
Set(Value As String)
Me._for = Value
End Set
End Property

Public Overrides Sub OnInit()
Me._for = Me.Id
End Sub
End Class


A Traveler said:
Hello all,

I have a control i wrote which inherits from asp:label. It worked fine.
It
extends asp:label with a "For" property which implements the "for"
attribute
of an html label tag (yes i know the latest framework vers. added this,
but
i want ot get mine right as a learning exp.).

I added a designer class which turns the For property into a dropdown
list
of the names of all the controls so you can simply pick one. When i added
the designer, the control no longer properly initialized its Text
property
when you drop it on a webform. I THINK the problem is in then designer's
Initialize sub.

The code for my designer is shown below.

When i drop the control on a page now, the text property starts out as ""
instead of with the controls id. If someone could tell me how to get this
to
properly initialize.

Thanks in advance,
- Arthur Dent.

===== BEGIN CODE ================================

Public Class LabelDesigner
Inherits ControlDesigner

Private pLabel As Label

Public Overrides Sub Initialize(ByVal component As IComponent)
MyBase.Initialize(component)
pLabel = CType(component, Label)
'pLabel.Text = pLabel.ID
End Sub

Public Property [For]() As String
Get
Return pLabel.For
End Get
Set(ByVal Value As String)
pLabel.For = Value
End Set
End Property

Protected Overrides Sub PreFilterProperties(ByVal properties As
IDictionary)
MyBase.PostFilterProperties(properties)
Dim prop As PropertyDescriptor = CType(properties("For"),
PropertyDescriptor)
If Not prop Is Nothing Then
prop = TypeDescriptor.CreateProperty(Me.GetType(), prop, New
Attribute() {New TypeConverterAttribute(GetType(ForConvertor))})
properties("For") = prop
End If
End Sub
End Class
 
W

WALDO

Try this

Public Overrides Sub OnInit()
Me._for = Me.Id
MyBase.OnInit() 'Forgot this line
End Sub
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top