【发布时间】:2011-09-26 02:23:57
【问题描述】:
有人可以解释向中继器内的用户控件提供数据的最简单方法吗?
我有以下几点:
默认.aspx
<!-- this.GetData() returns IEnumerable<Object> -->
<asp:Repeater runat="server" datasource='<%#this.GetData()%>'>
<ItemTemplate>
<my:CustomControl runat="server" datasource='<%#Container.DataItem %>
</ItemTemplate>
</asp:Repeater>
代码隐藏
protected void Page_Load(object sender, EventArgs e)
{
this.DataBind();
}
CustomControl.ascx
<!-- Object has property Title -->
<h1><%#this.DataSource.Title%></h1>
代码隐藏:
[System.ComponentModel.DefaultBindingProperty("DataSource")]
public partial class CustomControl : System.Web.UI.UserControl
{
public Item DataSource { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
var x = this.DataSource; //null here
}
protected void Page_PreRender(object sender, EventArgs e)
{
var x = this.DataSource; //still null
}
}
【问题讨论】:
-
是
this.GetData()在发生数据绑定的Page_Load之前或之后被调用。您可以将.GetData()移动到Page_Load中的数据绑定之前吗? -
this.GetData() 使用数据绑定表达式
#调用。所以叫ON数据绑定...
标签: asp.net data-binding user-controls