自定义控件中的嵌套标记

时间:2014-10-06 07:31:15

标签: c# asp.net asp.net-customcontrol

我创建了一个自定义控件来实现我们在整个地方使用的表。它已经工作了一段时间,现在我被要求添加一个新的列类型,允许任何有效的asp.net标记放入其中,并且该标记将在该表列中重新标记。

我创建了以下类:

    [PersistChildren(false), ParseChildren(true)]
    public class UserDefinedMarkupColumn : GridColumnBase
    {
        private Collection<WebControl> _innerMarkup = new Collection<WebControl>();

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public virtual Collection<WebControl> InnerMarkup { get { return _innerMarkup; } }

        public override System.Web.UI.WebControls.TableCell CreateCell(object dataSourceObject, GridColumnBase col, out CustomGridRow extraRow, Action<object, System.Web.UI.WebControls.CommandEventArgs> handler, bool isMobileLayout = false)
        {
            TableCell newCell = new TableCell();

            foreach (var ctrl in _innerMarkup)
            {
                newCell.Controls.Add(ctrl);
            }

            extraRow = null;
            return newCell;
        }
    }

以及该列的标记:

<CustomGrid:UserDefinedMarkupColumn ID="markup" HeaderText="Some Markup">
   <InnerMarkup>
      <asp:TextBox runat="server" />
   </InnerMarkup>
</CustomGrid:UserDefinedMarkupColumn>

问题在于,当我点击CreateCell时,_innerMarkup集合始终为空。我看到使用ControlsCollection的几个示例,但问题是GridColumnBase不会从Control继承,而是会创建一个TableCell

编辑:我还尝试添加PlaceHolder控件并使用它ControlsCollection,但它也是空的。

private PlaceHolder _innerControl = new PlaceHolder();

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public virtual ControlCollection InnerMarkup { get { return _innerControl.Controls; } }

0 个答案:

没有答案