我创建了一个自定义控件来实现我们在整个地方使用的表。它已经工作了一段时间,现在我被要求添加一个新的列类型,允许任何有效的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; } }