views:

96

answers:

0

I have a control that inherits CompositeDataBoundControl. Based on the values of the bound data, I create som dynamic controls. In order to preserve state, I need to recreate the control tree on every request(from CreateChildControls):

if(dataSource != null) {
     IEnumerator e = dataSource.GetEnumerator();
     if(e != null) {
         while(e.MoveNext()) {
            PaymentMethod pm = e.Current as PaymentMethod;
            PaymentMethodContainer dataContainer = new PaymentMethodContainer(e.Current as PaymentMethod);
             ITemplate itemTemplate = this.ItemTemplate;
             itemTemplate.InstantiateIn(dataContainer);
             if(pm != null) {
                Control c = pm.GetPaymentProvider().GetPaymentMethodControl();
                dataContainer.Controls.Add(c);
                c.ID = "paymentForm";
             }
          }
     }
}

The problem is that on postback the PaymentMethod object is null, so I have no method of knowing what kind of control to recreate. That means that the control state is ruined. If I hardcode the type of payment control to make, it works as expected:

UserControl userCtrl = (UserControl)Page.LoadControl("~/WAF/View/VisitControls/Shop/CreditCardForm.ascx");
dataContainer.Controls.Add(userCtrl);
userCtrl.ID = "CreditCardForm";

Any suggestions on how I can find out what payment method is used, so that I can create the correct control on postback? Any help is very appreciated!