Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Custom controls questions:


Message #1 by <zivf@m...> on Mon, 25 Mar 2002 12:48:49 +0200
1. How can I know (in the code running for the control) if the control 
is currently in design time or runtime.
2. I have overriden the CreateChildControls memeber function to create 
my controls, and everything seems to work fine, but the "Design time 
support" microsoft is so proud of does not seem to draw any HTML on the 
web-form designer, so instead of  a placeholder (like seen with 
user-controls) I see nothing.

anyone?
Message #2 by "Goh Mingkun" <mangokun@h...> on Tue, 26 Mar 2002 09:19:34
You can take a look at the following code, and see whether you can get 
anything useful out of it.
__________________________________________________
Imports System
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls

<DefaultProperty("Text"), ToolboxData("<{0}:LabeledTextbox runat=server></
{0}:LabeledTextbox>")> Public Class LabeledTextbox
    Inherits System.Web.UI.WebControls.WebControl

    Private _text As String
    Private _label As String = "Caption"

    <Bindable(True), Category("Appearance")> Public Property [Text]() As 
String
        Get
            Return _text
        End Get
        Set(ByVal Value As String)
            _text = Value
            If Me.ChildControlsCreated Then
                Me.Controls.Clear()
                CreateChildControls()
            End If
        End Set
    End Property

    <Category("Appearance")> Public Property [Label]() As String
        Get
            Return _label
        End Get
        Set(ByVal Value As String)
            _label = Value
            If Me.ChildControlsCreated Then
                Me.Controls.Clear()
                CreateChildControls()
            End If
        End Set
    End Property

    Protected Overrides Sub CreateChildControls()
        Dim _childText As New TextBox()
        _childText.Text = _text

        Me.Controls.Add(New LiteralControl(_label & ": "))
        Me.Controls.Add(_childText)
    End Sub

    Private Sub LabeledTextbox_Init(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles MyBase.Init
        Me.Height = New Unit(16)
        Me.Width = New Unit(256)
        ' Use EnsureChildControls to force a CreateChildControls call
        ' if it has't been done already.
        Me.EnsureChildControls()
    End Sub
End Class

Note:
1. The "Render" event should be removed.

2. If i say that only the _Init event is triggered automatically during 
Design time, therefore the statement "Me.EnsureChildControls()" will make 
sure that CreateChildControls() is triggered if ChildControlsCreated 
returns false.

3. The rest I leave it to you to find out and modify.
You can read up on the Documentation on "EnsureChildControls".

Previous message:
> 1. How can I know (in the code running for the control) if the control 
is currently in design time or runtime.
2. I have overriden the CreateChildControls memeber function to create 
my controls, and everything seems to work fine, but the "Design time 
support" microsoft is so proud of does not seem to draw any HTML on the 
web-form designer, so instead of  a placeholder (like seen with 
user-controls) I see nothing.

anyone?
Message #3 by "Trainer, Frank" <FTrainer@u...> on Tue, 26 Mar 2002 14:40:42 -0600

-----Original Message-----
From: Goh Mingkun [mailto:mangokun@h...] 
Sent: Tuesday, March 26, 2002 3:20 AM
To: ASP+
Subject: [aspx] Re: Custom controls questions:

You can take a look at the following code, and see whether you can get 
anything useful out of it.
__________________________________________________
Imports System
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls

<DefaultProperty("Text"), ToolboxData("<{0}:LabeledTextbox runat=server></
{0}:LabeledTextbox>")> Public Class LabeledTextbox
    Inherits System.Web.UI.WebControls.WebControl

    Private _text As String
    Private _label As String = "Caption"

    <Bindable(True), Category("Appearance")> Public Property [Text]() As 
String
        Get
            Return _text
        End Get
        Set(ByVal Value As String)
            _text = Value
            If Me.ChildControlsCreated Then
                Me.Controls.Clear()
                CreateChildControls()
            End If
        End Set
    End Property

    <Category("Appearance")> Public Property [Label]() As String
        Get
            Return _label
        End Get
        Set(ByVal Value As String)
            _label = Value
            If Me.ChildControlsCreated Then
                Me.Controls.Clear()
                CreateChildControls()
            End If
        End Set
    End Property

    Protected Overrides Sub CreateChildControls()
        Dim _childText As New TextBox()
        _childText.Text = _text

        Me.Controls.Add(New LiteralControl(_label & ": "))
        Me.Controls.Add(_childText)
    End Sub

    Private Sub LabeledTextbox_Init(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles MyBase.Init
        Me.Height = New Unit(16)
        Me.Width = New Unit(256)
        ' Use EnsureChildControls to force a CreateChildControls call
        ' if it has't been done already.
        Me.EnsureChildControls()
    End Sub
End Class

Note:
1. The "Render" event should be removed.

2. If i say that only the _Init event is triggered automatically during 
Design time, therefore the statement "Me.EnsureChildControls()" will make 
sure that CreateChildControls() is triggered if ChildControlsCreated 
returns false.

3. The rest I leave it to you to find out and modify.
You can read up on the Documentation on "EnsureChildControls".

Previous message:
> 1. How can I know (in the code running for the control) if the control 
is currently in design time or runtime.
2. I have overriden the CreateChildControls memeber function to create 
my controls, and everything seems to work fine, but the "Design time 
support" microsoft is so proud of does not seem to draw any HTML on the 
web-form designer, so instead of  a placeholder (like seen with 
user-controls) I see nothing.

anyone?

  Return to Index