Add Controls Dynamically In Presenter

Topics: Web Client Software Factory, User Forum
May 2, 2008 at 4:53 PM
Edited May 2, 2008 at 4:54 PM
I have created two user controls. One is going to contain 1 to 14 multiples of the other user control. How do you dynamically add UserControls in the Presenter?
May 2, 2008 at 6:20 PM
Edited May 2, 2008 at 6:21 PM
Use a Repeater on your user control and data bind appropriately? Your view would have method or property to set the data and data bind.


CwWatson wrote:
I have created two user controls. One is going to contain 1 to 14 multiples of the other user control. How do you dynamically add UserControls in the Presenter?

May 2, 2008 at 6:22 PM
Hi,

You may use the Controls collection to dynamically add controls to another control or the page. To get access to the View as a UserControl in the presenter you have to add, in the module’s project, a reference to System.Web and then you can cast the view to the type UserControl or Page if you are using the Page’s presenter, like this:

((UserControl)this.View).Controls.Add(item);

Please let me know if this helps.

Jonathan Cisneros
http://staff.southworks.net/blogs/jcisneros/default.aspx
May 2, 2008 at 8:05 PM


jonathan wrote:
Hi,

You may use the Controls collection to dynamically add controls to another control or the page. To get access to the View as a UserControl in the presenter you have to add, in the module’s project, a reference to System.Web and then you can cast the view to the type UserControl or Page if you are using the Page’s presenter, like this:

((UserControl)this.View).Controls.Add(item);

Please let me know if this helps.

Jonathan Cisneros
http://staff.southworks.net/blogs/jcisneros/default.aspx


I have tried a version of this. The scenario is I have a ShipmentDate.ascx that is a date control comprised of multiple textbox and such. Then I have a ShipmentDates.ascx control that depending on type of shipment will add multiple copies of the ShipmentDate.ascx. I tried to test this by simply using the following code in the presenter for the ShipmentDates.ascx control.

public override void OnViewInitialized()
{
View.AddDateControl((Control)new ShipmentDateViewPresenter().View);
}

The View.AddDateControl is a method on the ShipmentDates that would add a control to a Control Collection.

When I do this I get the following error:

System.ArgumentException: Invalid type owner for DynamicMethod
May 2, 2008 at 8:59 PM
I would think you want to avoid your presenter knowing about user controls/control collections etc. The presenter should be handling the logic about determining the type of shipment and asking the view to add shipment dates. Not all your user controls will require presenters. Perhaps, in your case your ShipmentDates.ascx view will have a presenter, but the ShipmentDate.ascx view will not. The ShipmentDate.ascx can still have an interface for manipulating the controls on it.


CwWatson wrote:


jonathan wrote:
Hi,

You may use the Controls collection to dynamically add controls to another control or the page. To get access to the View as a UserControl in the presenter you have to add, in the module’s project, a reference to System.Web and then you can cast the view to the type UserControl or Page if you are using the Page’s presenter, like this:

((UserControl)this.View).Controls.Add(item);

Please let me know if this helps.

Jonathan Cisneros
http://staff.southworks.net/blogs/jcisneros/default.aspx


I have tried a version of this. The scenario is I have a ShipmentDate.ascx that is a date control comprised of multiple textbox and such. Then I have a ShipmentDates.ascx control that depending on type of shipment will add multiple copies of the ShipmentDate.ascx. I tried to test this by simply using the following code in the presenter for the ShipmentDates.ascx control.

public override void OnViewInitialized()
{
View.AddDateControl((Control)new ShipmentDateViewPresenter().View);
}

The View.AddDateControl is a method on the ShipmentDates that would add a control to a Control Collection.

When I do this I get the following error:

System.ArgumentException: Invalid type owner for DynamicMethod

May 5, 2008 at 2:44 PM


pbolduc wrote:
I would think you want to avoid your presenter knowing about user controls/control collections etc. The presenter should be handling the logic about determining the type of shipment and asking the view to add shipment dates. Not all your user controls will require presenters. Perhaps, in your case your ShipmentDates.ascx view will have a presenter, but the ShipmentDate.ascx view will not. The ShipmentDate.ascx can still have an interface for manipulating the controls on it.


CwWatson wrote:


jonathan wrote:
Hi,

You may use the Controls collection to dynamically add controls to another control or the page. To get access to the View as a UserControl in the presenter you have to add, in the module’s project, a reference to System.Web and then you can cast the view to the type UserControl or Page if you are using the Page’s presenter, like this:

((UserControl)this.View).Controls.Add(item);

Please let me know if this helps.

Jonathan Cisneros
http://staff.southworks.net/blogs/jcisneros/default.aspx


I have tried a version of this. The scenario is I have a ShipmentDate.ascx that is a date control comprised of multiple textbox and such. Then I have a ShipmentDates.ascx control that depending on type of shipment will add multiple copies of the ShipmentDate.ascx. I tried to test this by simply using the following code in the presenter for the ShipmentDates.ascx control.

public override void OnViewInitialized()
{
View.AddDateControl((Control)new ShipmentDateViewPresenter().View);
}

The View.AddDateControl is a method on the ShipmentDates that would add a control to a Control Collection.

When I do this I get the following error:

System.ArgumentException: Invalid type owner for DynamicMethod



So I thought I would try a repeater with its DataSource as an ObjectContainerDataSource of Type ShipmentDate. Then I created a repeater with a usercontrol of Type ShipmentDate here is the markupthat I run.


<asp:Repeater ID="DatesRepeater" runat="server">
<ItemTemplate>
<uc1:ShipmentDate ID="ShipmentDate1" runat="server" />
</ItemTemplate>
</asp:Repeater>
<pp:ObjectContainerDataSource ID="DatesDataSource" runat="server" DataObjectTypeName="LC.Repository.BusinessEntities.ShipmentDate" />

Thie when I run it i get the following error:

Invalid type owner for DynamicMethod.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Invalid type owner for DynamicMethod.


Any suggestions?
Jun 5, 2008 at 9:35 AM
Edited Jun 5, 2008 at 9:53 AM
Hi,

I received the same error (Invalid type owner for DynamicMethod) but found a solution that works. I would love if a moderator or a team member could just help us here as I am unsure if this is the correct use.

To get rid of the Invalid type owner for DynamicMethod error I registered the shared controller in the ModuleInitializer of the parent.

So as an example I have a Template Business module and created a Foundation Business module to keep my common/shared controls.  So in the TemplateModuleInitializer's
Load method just add the shared controller.

container.RegisterTypeMapping<

IFoundationController, FoundationController>();

Also remember to register any services, which is made provision for by the AddModuleServices in the ModuleInitializer.

Further to that remember that you can't forward a property's value you set in the View to the Presenter until the control is initialized. You will get a
ArgumentNullException on the Presenter. Keep the state and forward it to the Presenter in the OnViewInitialized implementation.

 

 

If this is the correct use, I don't know, but it works.

Regards,

Anton Swanevelder