开发者

There has to be a way to get my data bound at the correct time

Say aspx page called theParent has a DataGrid control named theDataGrid and a UserControl named theUserControl , and theUserControl has a button named theUcButton .

Yes, I know, very imaginative naming.

When theUcButton is clicked , a session variable is changed.

This session variable is a select parameter for the datasource of theDataGrid.

However, because of the开发者_如何学编程 order of stuff called in the page lifecycle, when theUcButton is clicked and a postback is generated , theParent controls are loaded before theUserControl resets the session variable, and theDataGrid does not show the new data until the next postback .

How to I get what I want to work?

I am using 3.5 if that matters.


Here is example code for your user control to raise an event.

public partial class WebUserControl : System.Web.UI.UserControl{
public event EventHandler Updated;

protected void Button1_Click(object sender, EventArgs e)
{
    //do some work to this user control

    //raise the Updated event
    if (Updated != null)
        Updated(sender, e);
}}

Then from your .aspx page, you'll deal with the new event just like usual

    <uc1:WebUserControl ID="WebUserControl1" runat="server" OnUpdated="WebUserControl1_Updated" />

Code behind:

protected void WebUserControl1_Updated(object sender, EventArgs e)
{
    //handle the user control event
}


You can declare an event as a member of the UserControl class, raise it, and handle it in the Page class.

You can also use other events like Page.PreRender() to pick up things after the user control's events.


The easiest way would be to hold off on the databind until the later in the lifecycle, such as the Page.PreRender event as joelt suggested. If you do the bind then, you should have the session variables.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜