views:

42

answers:

3

I have a dynamic object built inside IronPython and I would like to build controls on my asp.net page dynamically based on what types of objects are nested inside my dynamic object:

dynamic variousComplexObjects = IronPythonApp.GetControls();
repeater.DataSource = variousComplexObjects;
repeater.DataBind();

Can someone write me a quick example of what to do next? I'm sure there is a tutorial out there doing something similar, but I'm having a bit of trouble googling it. Feel free to recommend me the correct keywords or point me in the direction of properly consuming DLR data in an asp.net app.

Thanks!!

A: 

Here's a thread entitled "Databind object with 'dynamic' properties". It involves creating customer get and set methods on the type.

It doesn't exactly fit with your scenario of C#'s dynamic and sourcing data from the DLR, but it might help.

Here's another thread entitled "Can I databind to a collection of Dynamic Class". The authored created a dynamic class using Reflection.Emit.

Please comment if any of these solutions fit your case. I'm also interested in the solution, but don't have the tolls to test, at the moment.

p.campbell
Thanks. That was sorta what I was looking for.
djmc
A: 

Thanks.. I guess nobody is coding in traditional asp.net web forms and dynamic objects. There's probably a way to do it.. but I just switched to MVC instead.

djmc
+2  A: 

Assuming you use .net 4, you can just use dynamic in your databound event.

repeater.ItemDataBound += OnItemDataBound;

protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
  dynamic dynObj = (dynamic)e.DataItem;
  string text = dynObj.Text; // Etc.
}

I'd probably have a type property or similar to check on - otherwise you're stuck with trying to use GetType() which I'm not certain whether works with IronPython.

Steffen
oh yeah hhaha, forgot about the OnItemDataBound. Cool, this is exactly what I was looking for. Thanks Steffen
djmc
Glad I could be of assistance :-)
Steffen