HowTo: Perform ASP.net action after page child controls are databound
While creating metadata entry/update pages for ClipFlair’s Activity and Clip Galleries I had the problem of figuring out how to do some initialization (from XML data, loaded from a filename based on the 1st item of a DataBound control), after all child controls of the ASP.net Page have been databound (from XmlDataSource).
Seems others have asked about that too, so I contributed my solution:
Based on ASP.net Page Life Cycle Events article I used:
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
if (!IsPostBack) //only at 1st load
UpdateSelection();
}
protected void UpdateSelection()
{
UpdateSelection(listItems.SelectedValue);
}
protected void listItems_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateSelection();
}
where UpdateSelection was loading XML data from a file selected at a dropdown list (which at start is pointing to index 0) and needed some CheckBoxLists on the page to have first gotten their items from other XML files so that they would allow the code to check items on them based on the XML data (from then on UpdateSelection is just getting called at the dropdownlist SelectedIndexChanged event – those do PostBacks so at PreRenderComplete we ignore them to avoid doing UpdateSelection twice)