【问题标题】:client-side values of dynamic control on postback回发动态控制的客户端值
【发布时间】:2010-09-16 23:48:56
【问题描述】:

我的自定义控件包含一个转发器,它将动态控件添加到 ItemDatabound 的占位符中。

我在访问动态控件的更新值时遇到问题,我已经在负责重建 Load 上的动态控件,但我首先需要了解用户所做的更改。我只是在理解生命周期中的哪个位置是访问更新的动态控件值的最佳位置时遇到了一些麻烦。

<Repeater>
    <ItemTemplate>
        <Label /><PlaceHolder />

【问题讨论】:

    标签: postback client-side placeholder


    【解决方案1】:

    如果有人偶然发现此页面,这就是让我走上正轨的网站: http://www.learning2code.net/Learn/2009/8/12/Adding-Controls-to-an-ASPNET-form-Dynamically.aspx

    在我的例子中,我放在占位符中的控件也是动态的(数据驱动的),它基于一个枚举类型,它决定了 CheckBox、ListBox、Textbox、RadDatePicker 等。将插入占位符中。

    由于我有一个带有多个占位符的中继器,而不是一个包含所有动态控件(如提供的链接)的占位符,因此我实现了我的解决方案,如下所示。

    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);   
                }
            }
        }
    

    您仍然必须实现循环通过中继器并从动态控件中提取更新的客户端值的代码。我希望这会有所帮助,解决这个问题确实需要做很多工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-23
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      相关资源
      最近更新 更多