Dynamically added TemplateFields in DetailsView do not set bound property

785 views Asked by At

I need to build a web page with a DetailsView which has a bunch of text boxes and drop down lists which bind to an ObjectContainerDataSource. This is for one-way binding using the InsertItemTemplate.

I can easily get this working for both text boxes and drop down lists using statically-defined fields, however my particular situation requires the fields to be added at runtime as the web page needs to be highly configurable.

For textboxes, this is easy via the BoundField class, however it all goes horribly wrong when I try to use the TemplateField for a Drop Down List. I have tried many different methods, including writing my own TemplateField class, and also setting the InsertItemTemplate directly using CompiledBindableTemplateBuilder. But no matter what I do, the basic issue is after the insert button is clicked, TemplateFields do not bind to the object data source. I believe this is because the InsertItemTemplate is null on postback (note that when all fields are declared in mark-up, InsertItemTemplate is not null). If anyone has done exactly what I'm trying to do, please reply, because I am now at the end of my tether with this.

I've reverted back to the simple TextBox inside a template field for proof of concept, but it will not work either. I've pasted what I've done below. In this case I'm trying to bind the TextBox input to a property called "CustomerRef". I think my issue may relate to how I'm setting the Text property, but I have not been able to find any other way to set the Text property to Bind(...) in code behind.

This is how I insert my templated field:

var tf = new TemplateField();
tf.InsertItemTemplate  = new CompiledBindableTemplateBuilder(delegate(Control container)
    {

        TextBox text = new TextBox();
        text.ID = "MyTextBoxId";
        text.Attributes.Add("Text", "<%# Bind(\"CustomerRef\") %>");
        container.Controls.Add(text);
    },
    delegate(Control container)
    {  // I've tried throwing an exception in here but it never gets executed

        OrderedDictionary dict = new OrderedDictionary();
        dict["CustomerRef"] = ((TextBox)container.Controls[0]).Text;
        return dict;
    });

And this is how I add the field to my DetailsView Fields list:

ShipmentSearchView.Fields.Add(tf);

I put some text in the textbox and press the insert button... then in my Event Handler for the insert event I try to get the data source like this:

 var dataSource = e.Instance as MyClassName;

At this point in debug environment I notice that the field in question no longer has the InsertItemTemplate - it is null, and MyCustomerRef is not set.

I have tried adding the fields in various places, in Page_Load() - both on every post back and only on first load; in Page_Init() and also in the Init() event of the Details View, but it never seems to make a difference how I do it. Thanks in advance for any help with this issue.

1

There are 1 answers

0
CoderJones On BEST ANSWER

I ended up writing my own custom user control to achieve this - it really isn't that hard to do. If anyone is interested in my solution I'd be happy to post it here. But going by the lack of comments against my original post, I'm guessing that what I'm doing on my web page is not something many people are attempting!