简体   繁体   中英

How can I use the PreLoad event from within a control?

I am trying to modify a control on a page to reduce session dependence. I currently do not have access to the page itself, only the control which primarily consists of a DataGrid. I am trying to retrieve the DataGrid information on postback so I can manipulate the data and rebind the grid.

The issue is that the page is calling a databind on the control before I can retrieve the data. (actually it is calling the databind on the tab control where my control is located.) This call is happening on the OnLoad event of the page, before the OnLoad of the control is called. I saw that the is a PreLoad event that occurs after the viewstate is loaded but before the OnLoad is called. However I am having issues accessing this event from my control. Is there anyway I can access this event so I can retrieve the data before the page overwrites it?

Add the following code to your control instead of the OnLoad. ( from here )

protected override void OnInit(System.EventArgs e)
{
    // this assigns Page_PreLoad as the event handler 
    // for the PreLoad event of the Control's Page property
    this.Page.PreLoad += Page_PreLoad;
    base.OnInit(e);
}

private void Page_PreLoad(object sender, System.EventArgs e)
{
    // do something here
}

This might help: MSDN - ASP.NET Page Life Cycle Overview

The image 1/3 of the way down sums it up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM