views:

8

answers:

1

My custom control contains a repeater that adds a dynamic control into a placeholder on ItemDatabound.

I'm having an issue accessing the updated value of the dynamic control, I am already taking care of rebuilding the dynamic controls on Load but I first need to get to the changes made by the user. I'm just having some trouble understanding where in the Lifecycle is the best place to have access to the updated dynamic control value.

<Repeater>
    <ItemTemplate>
        <Label /><PlaceHolder />
A: 

If anyone who stumbles across this page this is the website that got me on the right track: http://www.learning2code.net/Learn/2009/8/12/Adding-Controls-to-an-ASPNET-form-Dynamically.aspx

In my case the control I was putting inside my placeholder was also dynamic (data-driven) based on a enum type which determined if a CheckBox, ListBox, Textbox, RadDatePicker, ect. would be inserted in the placeholder.

Since I had a repeater with multiple placeholders instead of just one placeholder containing all of my dynamic controls like the link provided, I implemented my solution as follows.

On the method that adds your dynamic controls to the placeholder (ItemDataBound):  
1. Give the dynamic control a unique ID (string)  
2. Add the unique ID & enum type to the Dictionary<enum, string> that will be stored in the ViewState  

Override the LoadViewState method as follows (this will load your Dictionary<enum, string> array):  
    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
    }  

Override the OnLoad method to add the dynamic controls that were cleared on postback:
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (IsPostBack)
            AddDynamicControlsToPlaceholder();
    }

    private void AddDynamicControlsToPlaceholder()
    {
        foreach (RepeaterItem item in reapeater.Items)
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                var keyValue = DynamicDictValues.ElementAt(item.ItemIndex);
                var plhDynControl = item.FindControl("plhDynControl") as PlaceHolder;
                //CreateDynamicControl method uses the key to build a specific typeof control  
                //and the value is assigned to the controls ID property.
                var dynamicControl = CreateDynamicControl(keyValue);
                plhItemValue.Controls.Add(dynamicControl);   
            }
        }
    }

You still have to implement the code that loops through the repeater and pulls the updated client-side values from the dynamic controls. I hope this helps, it really took a lot of work getting this one solved.

Neomoon