views:

226

answers:

1

I'm using ASP.NET Dynamic Data functionality to do something a little weird. Namely, create a dynamic list of fields as children of the main object.

So basically I have Ticket.Fields. The main page lists all the fields for Ticket, and the Fields property has a DynamicControl that generates a list of controls to collect more data.

The tricky part is that this list ALSO uses Dynamic Data to generate the controls, so each field can be any of the defined FieldTemplates... meaning I don't necessarily know what the actual data control will be when I try to get the value.

So, how do I get the value of a DynamicControl?

Do I need to create a new subclass of FieldTemplate that provides a means to get at the value?

A: 

It's amazing what a good nights sleep will do.

Well, that and going to extend FieldTemplate only to realize it already exposes the data control for you...

foreach (ListViewDataItem i in lvFields.Items)
    {         
        var val = i.FindDynamicControlRecursive("ValueText") as DynamicControl;
        var ft = val.FindFieldTemplate("ValueText") as FieldTemplateUserControl;

        ParseControl(ft.DataControl)
    }

ParseControl is made up here, but I actually have a class laying around that can handle that part for me... so hopefully future readers will at least get the idea here.

The important bit is to cast the result of FindFieldTemplate to FieldTemplateUserControl. For some reason it just returns Control, even though it seems like it should only be able to return FieldTemplateUserControls. I guess you could override what field templates are by changing the field generator class... but still.

Telos

related questions