I have solved the problem.
The following code sample will programmatically add a control to a ASPX Web Form from the INIT method of another control.
It solves the problem of the designer not displaying the control.
Enjoy!
'Get a Handle of the Designer
Dim host As IDesignerHost = CType(Me.Page.Site.GetService(GetType(IDesignerHost)), IDesignerHost)
Dim myTransaction As System.ComponentModel.Design.DesignerTransaction = host.CreateTransaction
MsgBox("Before adding the control there are " & host.Container.Components.Count.ToString & " controls on the designer")
MsgBox("Before adding the control there are " & Me.Page.Controls.Count.ToString & " controls on the page")
'Create a component
Dim newItem As IComponent = host.CreateComponent(GetType(System.Web.UI.WebControls.LinkButton))
Dim dcd As IDesigner = host.GetDesigner(newItem)
'This method of creating the component would also work
'Dim newItem2 As IComponent = CType(System.Activator.CreateInstance(GetType(System.Web.UI.WebControls.LinkButton)), System.Web.UI.WebControls.LinkButton)
'Set properties for the newly created control
Dim myNewLinkButton As System.Web.UI.WebControls.LinkButton = DirectCast(newItem, System.Web.UI.WebControls.LinkButton)
myNewLinkButton.Text = "This is a sample text"
myNewLinkButton.ID = "t" & Now.Millisecond.ToString 'Do not forget to assign an ID
dcd.Component.Site.Name = myNewLinkButton.ID 'If you do not do this, when you manually add a control of the same type you'd get an error
'host.Container.Add(myNewLinkButton) 'Only required if you had used Activator.CreateInstance to create the component
CType(host.RootComponent, System.Web.UI.Page).Controls.Add(CType(myNewLinkButton, System.Web.UI.WebControls.LinkButton))
'This is the magical piece.
'This solves the problem of the designer not showing up the control.
'It will cause the control to show up at designtime on the designer and on the ASPX page
'You have to inform the designer of each property that needs to be persisted. The most important is the ID
Dim designer As System.Web.UI.Design.ControlDesigner = dcd
Dim pd As PropertyDescriptor = TypeDescriptor.GetProperties(myNewLinkButton)("ID")
Dim ccea As ComponentChangedEventArgs = New ComponentChangedEventArgs(myNewLinkButton, pd, "", myNewLinkButton.ID)
designer.OnComponentChanged(myNewLinkButton, ccea)
'Now, the text property
pd = TypeDescriptor.GetProperties(myNewLinkButton)("Text")
ccea = New ComponentChangedEventArgs(myNewLinkButton, pd, "", myNewLinkButton.Text)
designer.OnComponentChanged(myNewLinkButton, ccea)
'Finaly Commit
myTransaction.Commit()
MsgBox("After adding the controls there are " & host.Container.Components.Count.ToString & " controls on the designer")
MsgBox("After adding the control there are " & Me.Page.Controls.Count.ToString & " controls on the page")
'Select the control on the designer
Dim sel As ISelectionService = DirectCast(host.RootComponent.Site.GetService(GetType(ISelectionService)), ISelectionService)
sel.SetSelectedComponents(New IComponent() {myNewLinkButton}
From http://www.developmentnow.com/groups/viewthread.aspx?newsgroupid=32&threadid=12360
Posted via DevelopmentNow.com Group
http://www.developmentnow.com