gpt4 book ai didi

asp.net - 如何向中继器内的用户控件提供 'data'?

转载 作者:行者123 更新时间:2023-12-04 20:21:03 25 4
gpt4 key购买 nike

有人可以解释向 提供数据的最简单方法吗?用户 中继器内部的控制?

我有以下内容:

默认.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();
}

自定义控件.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
}
}

最佳答案

您可以向用户控件添加属性,然后在数据绑定(bind)期间设置这些属性。

像这样:

<!-- this.GetData() returns IEnumerable<Object> -->
<asp:Repeater runat="server" datasource='<%#this.GetData()%>'>
<ItemTemplate>
<my:CustomControl runat="server" title='<%#Container.DataItem.title %>
</ItemTemplate>
</asp:Repeater>

代码隐藏
protected void Page_Load(object sender, EventArgs e)
{
this.DataBind();
}

自定义控件.ascx
<!-- Object has property Title -->
<h1><%#this.Title%></h1>

代码隐藏:
[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
}
}

关于asp.net - 如何向中继器内的用户控件提供 'data'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6537143/

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