【发布时间】:2011-10-17 19:51:08
【问题描述】:
我已将项目列表设置为转发器的数据源,并创建了自定义模板。有没有办法直接访问模板中的当前绑定项? 下面的示例代码
class MyObject
{
public string Somevalue{get;set;}
}
我的 Page_Load 中的代码
List<MyObject> selections = new List<MyObject>();
selections.Add(new MyObject());
Repeater1.ItemTemplate = new MyObjectTemplate();
Repeater1.DataSource = selections;
Repeater1.DataBind();
模板
public class MyObjectTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
//Get the current item
MyObject o = ????? as MyObject;
string txt = "<h1>"+ o.Somevalue + "</h1>";
LiteralControl h1 = new
LiteralControl(txt);
container.Controls.Add(h1);
}
}
我知道,如果我只是想在转发器中显示“Somevalue”的值,会有更简单的方法来实现这一点,但显示哪些值以及如何显示的实际逻辑更复杂。
【问题讨论】:
-
InstantiateIn实际上会被调用,即使中继器没有数据绑定(在回发上)来重新创建项目,因此没有数据项。此链接可能对您有所帮助:community.devexpress.com/forums/t/70674.aspx
标签: asp.net templates repeater