DataBind覆盖中WebUserControl的属性为null

时间:2013-04-10 15:16:51

标签: asp.net

我的WebUserControl与DataBind覆盖:

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

此控件位于具有声明性有界属性的转发器中:

<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>

现在我在转发器上调用DataBind():

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

在overriden控件的DataBind()方法中我没有正确设置属性DataSource和Text,它们是null,为什么?

1 个答案:

答案 0 :(得分:0)

如果WebUserControl1中只有一个转发器控件,则更容易绑定子转发器Repeater.ItemDataBound Event中的子转发器控件。

以下是样本 -

enter image description here

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;
    }
}

原始问题的解决方案

删除公开覆盖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);
  }
}