绑定控件内的 ASP.Net 数据绑定

发布于 2024-12-12 02:02:48 字数 969 浏览 2 评论 0原文

我今天对 ASP.Net 数据绑定有点感兴趣,基本上我有两个嵌套控件,以及一个带有我希望绑定的自己内部集合的对象集合...

所以,假设我正在使用两个中继器,例如这个->

    <asp:Repeater ID="Repeater1">
      <ItemTemplate>
        <asp:Label runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "HeaderText")%>'>
        </asp:Label>
        <asp:Repeater ID="Repeater2">
          <asp:Label runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "DetailText")%>'>
          </asp:Label>
        </asp:Repeater>
      </ItemTemplate>
    </asp:Repeater>

我的对象看起来像这样:

    public class parent
    {
      public string HeaderText {get;set;}
      public List<child> children {get;set;}
    }
    public class child
    {
      public string DetailText {get;set;}
    }

如何绑定内部中继器?我猜我需要设置 &将“Repeater2”的数据源绑定在aspx中的某个位置作为“parent”的“children”属性?

有人能指出我正确的方向吗?

谢谢

I'm having a littl fun with ASP.Net data binding today, basically I have two nested controls, and an collection of objects with their own internal collections which I wish to bind...

So, say I'm using two repeaters like this ->

    <asp:Repeater ID="Repeater1">
      <ItemTemplate>
        <asp:Label runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "HeaderText")%>'>
        </asp:Label>
        <asp:Repeater ID="Repeater2">
          <asp:Label runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "DetailText")%>'>
          </asp:Label>
        </asp:Repeater>
      </ItemTemplate>
    </asp:Repeater>

And my objects look like this:

    public class parent
    {
      public string HeaderText {get;set;}
      public List<child> children {get;set;}
    }
    public class child
    {
      public string DetailText {get;set;}
    }

How do I bind the inner repeater? I'm guessing that I need to set & bind the datasource of 'Repeater2' somewhere in the aspx to be the 'children' property of 'parent'?

Can someone point me in the right direction?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦行僧 2024-12-19 02:02:48

在主转发器 ItemDataBound 事件中绑定嵌套转发器。

http://msdn.microsoft.com /en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx

在这里您可以找到控件 (FindControl) 并绑定到它。

它会是这样的:

<asp:Repeater ID="Repeater1" OnItemDataBound="Repeater1_ItemDataBound">


void Repeater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
    Repeater Rep2 = e.Item.FindControl("Repeater2");
    Rep2.DataSource = //datasource here
    Rep2.DataBind();         
}    

Bind the nested repeater in the main repeater ItemDataBound event.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx

Here you can find the control (FindControl) and bind to it.

It would be something like:

<asp:Repeater ID="Repeater1" OnItemDataBound="Repeater1_ItemDataBound">


void Repeater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
    Repeater Rep2 = e.Item.FindControl("Repeater2");
    Rep2.DataSource = //datasource here
    Rep2.DataBind();         
}    
原谅过去的我 2024-12-19 02:02:48

您可以在外部转发器的 ItemDatabound 事件中绑定内部转发器:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Repeater innerRepeater = e.Item.FindControl("InnerRepeater1") as Repeater;
    if (innerRepeater != null)
    {
        innerRepeater.DataSource = GetSomeData();
        innerRepeater.DataBind();
    }
}

如果需要,使用 ListViewDataList 可能会更容易使用外部数据绑定控件中的数据来绑定内部数据绑定控件,因为您可以指定数据键。

<asp:ListView ID="ListView1" runat="server" DataKeyNames="SomeColumn" ...>

隐藏代码:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    ListView innerList = e.Item.FindControl("InnerList1") as ListView;
    if (innerList != null)
    {
        innerList.DataSource = GetSomeData((int)ListView1.DataKeys[ListView1.Items.IndexOf(e.Item)]["SomeColumn"]);
        innerList.DataBind();
    }
}

You would bind the inner repeater in the ItemDatabound event of the outer repeater:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Repeater innerRepeater = e.Item.FindControl("InnerRepeater1") as Repeater;
    if (innerRepeater != null)
    {
        innerRepeater.DataSource = GetSomeData();
        innerRepeater.DataBind();
    }
}

It might be easier to use a ListView or DataList if you need to use data from the outer databound control to bind the inner databound control, because you can specify datakeys.

<asp:ListView ID="ListView1" runat="server" DataKeyNames="SomeColumn" ...>

Code-behind:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    ListView innerList = e.Item.FindControl("InnerList1") as ListView;
    if (innerList != null)
    {
        innerList.DataSource = GetSomeData((int)ListView1.DataKeys[ListView1.Items.IndexOf(e.Item)]["SomeColumn"]);
        innerList.DataBind();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文