2

I have WebUserControl with DataBind override:

public partial class WebUserControl1 : System.Web.UI.UserControl
{
    public object DataSource { get; set; }

    public string Text { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public override void DataBind()
    {
        // *** there when called, properties are null, why? ***
        repeater2.DataSource = DataSource;
        repeater2.DataBind();
    }
}

This control is in repeater with declarative bounded properties:

<asp:Repeater ID="repeater" runat="server">
    <ItemTemplate>
        <WebUserControl1 runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem, "levels") %>' Text='<%# DataBinder.Eval(Container.DataItem, "Text") %>' />
    </ItemTemplate>
</asp:Repeater>

Now when i call DataBind() on repeater:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ... data query
        repeater.DataSource = items;
        repeater.DataBind();
    }
}

in overriden control's DataBind() method i don't have properly setted properties DataSource and Text, they are null, why?

4

1 回答 1

0

如果您只有一个中继器控件,则在父中继器的Repeater.ItemDataBound 事件中绑定该子中继器控件WebUserControl1会更容易。

这是样本 -

在此处输入图像描述

ASPX

<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ParentRepeater_ItemDataBound">
    <ItemTemplate>
        <asp:Label runat="server" ID="StreetLabel" /><br/>
        <asp:Repeater ID="ChildRepeater" runat="server">
            <ItemTemplate>
                <asp:Label runat="server" ID="FirstNameLabel" />
                <asp:Label runat="server" ID="LastNameLabel" />
            </ItemTemplate>
        </asp:Repeater><hr/>
    </ItemTemplate>
</asp:Repeater>

代码背后

public class Address
{
    public int Id { get; set; }
    public string Street { get; set; }
    public List<User> Users { get; set; }
}

public class User
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ParentRepeater.DataSource = new List<Address>
            {
                new Address
                    {
                        Id = 1,
                        Street = "1st Street",
                        Users = new List<User>()
                            {
                                new User {Id = 1, FirstName = "John", LastName = "Doe"},
                                new User {Id = 1, FirstName = "Marry", LastName = "Doe"}
                            }
                    },
                    new Address
                    {
                        Id = 2,
                        Street = "2nd Street",
                        Users = new List<User>()
                            {
                                new User {Id = 1, FirstName = "Eric", LastName = "Newton"},
                                new User {Id = 1, FirstName = "John", LastName = "Newton"}
                            }
                    }

            };
        ParentRepeater.DataBind();
    }
}

protected void ParentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var address = e.Item.DataItem as Address;

        var streetLabel = e.Item.FindControl("StreetLabel") as Label;
        streetLabel.Text = address.Street;

        var repeater = e.Item.FindControl("ChildRepeater") as Repeater;
        repeater.ItemDataBound += ChildRepeater_ItemDataBound;

        repeater.DataSource = address.Users;
        repeater.DataBind();
    }
}

protected void ChildRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var user = e.Item.DataItem as User;

        var firstNameLabel = e.Item.FindControl("FirstNameLabel") as Label;
        firstNameLabel.Text = user.FirstName;

        var lastNameLabel = e.Item.FindControl("LastNameLabel") as Label;
        lastNameLabel.Text = user.LastName;
    }
}

原始问题的解决方案

删除public override void DataBind()事件并在PreRender 事件中绑定repeaters2

public partial class WebUserControl1 : System.Web.UI.UserControl
{
  protected override void OnPreRender(EventArgs e)
  {
    repeater2.DataSource = DataSource;
    repeater2.DataBind();

    base.OnPreRender(e);
  }
}
于 2013-04-10T16:17:51.790 回答