Rop*_*tah 5 asp.net data-binding user-controls
有人可以解释向转发器内的用户控件提供数据的最简单方法吗?
我有以下内容:
<!-- this.GetData() returns IEnumerable<Object> -->
<asp:Repeater runat="server" datasource='<%#this.GetData()%>'>
<ItemTemplate>
<my:CustomControl runat="server" datasource='<%#Container.DataItem %>
</ItemTemplate>
</asp:Repeater>
Run Code Online (Sandbox Code Playgroud)
代码隐藏
protected void Page_Load(object sender, EventArgs e)
{
this.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
<!-- Object has property Title -->
<h1><%#this.DataSource.Title%></h1>
Run Code Online (Sandbox Code Playgroud)
代码隐藏:
[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
}
}
Run Code Online (Sandbox Code Playgroud)
您可以向用户控件添加属性,然后在数据绑定期间设置这些属性.
像这样:
<!-- this.GetData() returns IEnumerable<Object> -->
<asp:Repeater runat="server" datasource='<%#this.GetData()%>'>
<ItemTemplate>
<my:CustomControl runat="server" title='<%#Container.DataItem.title %>
</ItemTemplate>
</asp:Repeater>
Run Code Online (Sandbox Code Playgroud)
代码隐藏
protected void Page_Load(object sender, EventArgs e)
{
this.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
CustomControl.ascx
<!-- Object has property Title -->
<h1><%#this.Title%></h1>
Run Code Online (Sandbox Code Playgroud)
代码隐藏:
[System.ComponentModel.DefaultBindingProperty("DataSource")]
public partial class CustomControl : System.Web.UI.UserControl
{
public Item DataSource { get; set; }
public string title { 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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6228 次 |
| 最近记录: |