How to detect when a Component is added to a WebForm

D

David

Hello
I have made a component (MyWebForm) that inherits Page. This component
have a method called 'ComponentAdded' that shows a msgbox if it is in
design mode:

Public Class MyWebForm
Inherits System.Web.UI.Page

Public Sub ComponentAdded()
If ((Not Me.Site Is Nothing) AndAlso (Me.Site.DesignMode)) Then
MsgBox("Method ComponentAdded")
End If
End Sub
End Class

I have another component (MyCotrol) that inherits WebControl, that
execute ComponentAdded method when it is added in MyWebForm in design
mode:

<Designer(GetType(DesignerMyCotrol)), ToolboxData("<{0}:MyCotrol
runat=server></{0}:MyCotrol>")> _
Public Class MyCotrol
Inherits WebControl
End Class

Public Class DesignerMyCobtrol
Inherits System.Web.UI.Design.ControlDesigner

Private control As IurisTextBox

Public Overrides Sub OnSetParent()
Try
control = CType(MyBase.Component, MyControl)
If (control.Page.GetType().Name = "MyWebForm") Then
CType(control.Page, MyWebForm).ComponentAdded()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Public Overrides Function GetDesignTimeHtml() As String
Return "My Control"
End Function
End Class


I want that when MyControl is added in MyWebForm, excute the
ComponentAdded method.
This code not works very well. When MyControl is added, ComponentAdded
method show an exception saying that some object has not been
inicialized.
But if I save the aspx, close it, and open again, the ComponentAdded
method show the msgbox well.
I think that OnSetParent is not the right method to put this code, but
I haven't find any other. Is there any event that execute when a
control is added in a webform?

Thanks
David
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top