开发者

Bind data to repeater on button click from user control in another repeater

I have a Page with 2 repeaters on it and each repeater contains a user control.

The idea is that when you click a button on the usercontrol in the repeater on the left, data is retrieved开发者_如何学JAVA from the database and set as the datasource for the repeater on the right. I use event bubbling to pass the event from the user control to the page so I can get the data and set it for the other repeater.

My problem is that the ItemDataBound event handler doesn't get called again after setting the repeater source in the event handler of the page for the user control button event.

Here's the layout of the page

<div id="leftDiv">
    <asp:Repeater ID="rptUser" runat="server">
        <ItemTemplate>
            <custom:UserInfo ID="cstUserInfo" runat="server" />
            <br />
        </ItemTemplate>
    </asp:Repeater>
</div>
<div id="rightDiv">
    <asp:Repeater ID="rptMessages" runat="server">
        <ItemTemplate>
            <custom:MessageDetails ID="cstMessageDetails" runat="server" />
        </ItemTemplate>
    </asp:Repeater>
</div>

And here's the event handler for the user control button click event.

private void cstUserInfo_ShowMessagesClicked(object sender, EventArgs e)
{
    UserInfoControl userInfoControl = sender as UserInfoControl;
    //Get the messages
    messageManager = new MessageManager();
    List<Messages> messages = messageManager.GetMessageDetailsByUserId(userInfoControl.CurrentUser.Id).ToList();

    rptTimeline.DataSource = messages;
    rptTimeline.ItemDataBound += new RepeaterItemEventHandler(rptMessages_ItemDataBound);
    rptTimeline.DataBind();
}

When I click the button on the user control the following happens:

  1. Page_Load of the page executes, which sets the default source for the second repeater
  2. The ItemDataBound handler for the repeater executes which sets the properties of the user control with the data
  3. Page_Load of the user controls executes which sets labels on the user control with its property values.
  4. The event handler cstUserInfo_ShowMessagesClicked executes that sets a new datasource for the repeater
  5. The Page_load of the user control executes again and throws an exception because the properties are not set yet

Is there any way to bind the new data to the second repeater when a button on the user control in the first repeater is pressed?


You'll want to use ItemCreated.

repeater.ItemCreated += new ItemCreatedEventHandler()

It's called each time an item is created to be bound to the repeater.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜