gpt4 book ai didi

asp.net - 使用 ITemplate 对自定义控件进行数据绑定(bind)

转载 作者:行者123 更新时间:2023-12-04 15:27:17 25 4
gpt4 key购买 nike

这是我的自定义服务器控件的示例代码:

[Designer(typeof(ContainerControlDesigner))]
[ToolboxData("<{0}:BlocArrondi runat=server><ContenuPrincipal></ContenuPrincipal></{0}:BlocArrondi>")]
public class BlocArrondi : WebControl
{
private ITemplate _ContenuPrincipal;
protected Panel _PanelContenuPrincipal = new Panel();

public BlocArrondi()
: base(HtmlTextWriterTag.Div)
{

}

[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateInstance(TemplateInstance.Single)]
public ITemplate ContenuPrincipal
{
get { return _ContenuPrincipal; }
set { _ContenuPrincipal = value; }
}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
_PanelContenuPrincipal.ID = "PanelPrincipal";
this.Controls.Add(_PanelContenuPrincipal);

if (_ContenuPrincipal != null)
_ContenuPrincipal.InstantiateIn(_PanelContenuPrincipal);
}
}

这里是实现:
<controls:BlocArrondi runat="server">
<ContenuPrincipal>
<asp:Label id="LabelInfo" runat="server" />
</ContenuPrincipal>
</controls:BlocArrondi>

我的标签 LabelInfo 可以在后面的代码中访问,太棒了!

但是,如果我在 Repeater 或 ListView 中使用我的自定义控件,我不能在 ContenuPrincipal 模板中使用 Container.DataItem 属性:
<asp:Repeater id="RepeaterInfos" runat="server">
<ItemTemplate>
<controls:BlocArrondi runat="server">
<ContenuPrincipal>
<asp:Label runat="server" Text="<%# (Container.DataItem as MsgInfo).DisplayMessage() " />
</ContenuPrincipal>
</controls:BlocArrondi>
</ItemTemplate>
</asp:Repeater>

错误信息:

'System.Web.UI.Control' does not contain a definition for 'DataItem' and no extension method 'DataItem' accepting a first argument of type 'System.Web.UI.Control' could be found (are you missing a using directive or an assembly reference?)



如何在我的控件的 ContenuPrincipal 模板中使用 Container.DataItem 属性?

最佳答案

我用继承自 IDataItemContainer 的自定义 TemplateContainer 解决了我的问题:

    public class BlocArrondiContainer : Panel, IDataItemContainer

改变 :
    protected Panel _PanelContenuPrincipal = new Panel();

[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateInstance(TemplateInstance.Single)]
public ITemplate ContenuPrincipal
{
get { return _ContenuPrincipal; }
set { _ContenuPrincipal = value; }
}

经过 :
    protected BlocArrondiContainer _PanelContenuPrincipal = new BlocArrondiContainer ();

[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateInstance(TemplateInstance.Single)]
[TemplateContainer(typeof(BlocArrondiContainer))]
public ITemplate ContenuPrincipal
{
get { return _ContenuPrincipal; }
set { _ContenuPrincipal = value; }
}

覆盖 BlocArrondi 类的 OnDataBinding 方法:
    protected override void OnDataBinding(System.EventArgs e)
{
if (NamingContainer is IDataItemContainer)
_PanelContenuPrincipal.DataItem = (NamingContainer as IDataItemContainer).DataItem;

base.OnDataBinding(e);
}

关于asp.net - 使用 ITemplate 对自定义控件进行数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4334670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com