views:

2859

answers:

4

What would be the best way to call a method in the code-behind of parent page from the code behind of the child page in the ASP.NET 2.0 Web Site model?

Scenario: User clicks a link in the parent page to view a data record's detail in child page (The parent and child are tow seperate pages). The user will modified the data in the client page. Once the child page has processed the update, the child page will close and I need to rebind the parent page to reflect the update.

I did something similar, but it was calling the parent page from a user control that was included in the parent page

if(Page.GetType().ToString().Contains("ASP.Default_aspx"))
{
    MethodInfo MyMethod = this.Parent.TemplateControl.GetType().GetMethod("MyMethod");
    MyMethod.Invoke(this.Page, null);
    MyMethod = null;
}

UPDATE: I have to do this from the code behind because the child page closes after the data has been updated.

+1  A: 

Easiest way would be to do the whole thing on one page using a multiview or some such thing.

Other then that, with javascript you can call

document.opener.location.href = "url";

to change the url on the parent page. You could just keep it the same, or stick stuff in the query string and muck with those values on Page_Load

Matt Briggs
A: 

If you're opening the child page in a modal pop-up, you can access window.returnValue on the parent page (via JavaScript) and then invoke a page refresh or an Ajaxy rebind call.

Check this page out for how to return value from modal pop-up and do something based on that value on parent page.

However, if you have the option, I'd get away from opening the edit form in a separate page. I'd put the edit form in a user control and show/hide it a la lightbox style.

Kon
+2  A: 

I was able to accomplish this by doing the following solution

Michael Kniskern
A: 

Not sure if this is exactly what you want but you could have used cross page posting for this. This allows you to post back to the parent page from the child page with the advantage of having access to the child page's ViewState. To do this you set the PostBackUrl property of a button to the child page. In the parent page you can then access the PreviousPage property to retrieve values from the child page. So in the child page have a button such as:

<asp:Button ID="Button1" runat="server" Text="Update data" 
        PostBackUrl="~/Parent.aspx" onclick="Button1_Click" />

Then in the Parent.aspx Page_Load event:

protected void Page_Load(object sender, EventArgs e) {

    if (IsCrossPagePostBack) {

        Page prevPage = this.PreviousPage;
        string myVal = prevPage.FindControl("myTextBox1").ToString();
        string myVal2 = prevPage.FindControl("myTextBox2").ToString();
        //etc

    }
}
Ciaran Bruen