如何根据项目的类型选择具有多个模板的转发器控件?
这就是我目前的情况:
我的中继课程:
[ToolboxData("<{0}:LifestreamRepeater runat=server>")]
public class LifestreamRepeater : Repeater
{
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate TwitterTemplate {get; set;}
protected override void OnDataBinding(EventArgs e)
{
//base.OnDataBinding(e);
foreach (var item in (IEnumerable<LifestreamItem>)this.DataSource)
{
if (item is LifestreamTwitterItem)
{
LifestreamRepeaterItem ri = new LifestreamRepeaterItem(item);
TwitterTemplate.InstantiateIn(item);
}
else
{
ItemTemplate.InstantiateIn(item);
}
}
}
}
和前端:
<lfs:LifestreamRepeater runat="server" ID="repeater1">
<TwitterTemplate>
<div class="Lifestream Twitter Item">
<h4> <%# DataBinder.Eval(Container.DataItem, "Title")%> </h4>
<p> <%# DataBinder.Eval(Container.DataItem, "Body")%> </p>
</div>
</TwitterTemplate>
<ItemTemplate>
<div class="Lifestream Item">
<h2> <%# DataBinder.Eval(Container.DataItem, "Title")%> </h2>
<p> <%# DataBinder.Eval(Container.DataItem, "Body")%> </p>
</div>
</ItemTemplate>
</lfs:LifestreamRepeater>
然后我将转发器控件绑定到一个IEnumerable的LifestreamItem,它是多个不同社交网络帖子类型的基类,因此可能有TwitterLifestreamItem和VimeoLifestreamItem,我希望转发器选择不同的模板,具有不同的可能值,具体取决于在dataitem上。
答案 0 :(得分:1)
我不认为ASP.NET中有任何内容允许这样做,但你可以这样做:
<asp:Repeater ID="myRepeater" runat="server" OnItemDataBound="myRepeater_ItemDataBound">
<ItemTemplate>
<asp:PlaceHolder ID="twitterTemplate" Visible="false" runat="server">
<asp:Literal ID="fooTwitterControl" runat="server" />
</asp:PlaceHolder>
<asp:PlaceHolder ID="itemTemplate" Visible="false" runat="server">
<asp:Literal ID="fooItemControl" runat="server" />
</asp:PlaceHolder>
</ItemTemplate>
</asp:Repeater>
ItemDataBound
事件:
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (ListItemType.Item == e.Item.ItemType || ListItemType.AlternatingItem == e.Item.ItemType)
{
PlaceHolder twitterTemplate = (PlaceHolder)e.Item.FindControl("twitterTemplate");
PlaceHolder itemTemplate = (PlaceHolder)e.Item.FindControl("itemTemplate");
var item = (LifestreamItem)e.Item.DataItem;
if (item is LifestreamTwitterItem)
{
twitterTemplate.Visible = true;
Literal fooTwitterControl = (Literal)e.Item.FindControl("fooTwitterControl");
// Load all twitter related controls + populate
}
else
{
itemTemplate.Visible = true;
Literal fooItemControl = (Literal)e.Item.FindControl("fooItemControl");
// Load all non-twitter controls + populate
}
}
}
基本上,ItemTemplates
会成为占位符,并根据您重复的类型启用/禁用。
答案 1 :(得分:0)
似乎解决方案是覆盖DataBind方法,如下所示:
public override void DataBind()
{
foreach (var item in (IEnumerable<LifestreamItem>)this.DataSource)
{
if (item is LifestreamTwitterItem)
{
TwitterTemplate.InstantiateIn(item); // instantiate inside the item which is also a control.
}
else
{
ItemTemplate.InstantiateIn(item);
}
item.DataBind(); // bind the item
Controls.Add(item); // add the item to the repeater
}
}