Michael Kniskern
Michael Kniskern

Reputation: 25270

Calling a parent page code behind method from a child page code behind - ASP.NET 2.0 Page Inheritance model

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.

Upvotes: 0

Views: 12914

Answers (3)

Ciarán Bruen
Ciarán Bruen

Reputation: 5341

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

    }
}

Upvotes: 0

Kon
Kon

Reputation: 27441

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.

Upvotes: 0

Matt Briggs
Matt Briggs

Reputation: 42218

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

Upvotes: 1

Related Questions