C# 从后面的子页面代码调用父页面代码后面的方法 - ASP.NET 2.0 页面继承模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/375977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:08:32  来源:igfitidea点击:

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

c#asp.netweb-site-project

提问by Michael Kniskern

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?

从 ASP.NET 2.0 网站模型中子页面的代码隐藏在父页面的代码隐藏中调用方法的最佳方法是什么?

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.

更新:我必须从后面的代码中执行此操作,因为子页面在数据更新后关闭。

回答by Matt Briggs

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

除此之外,您可以使用javascript调用

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

更改父页面上的 url。您可以保持不变,或者在查询字符串中粘贴内容并在 Page_Load 上使用这些值

回答by Kon

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

如果您在模态弹出窗口中打开子页面,您可以访问window.returnValue父页面(通过 JavaScript),然后调用页面刷新或 Ajaxy 重新绑定调用。

Check this pageout 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.

但是,如果您可以选择,我不会在单独的页面中打开编辑表单。我将编辑表单放在用户控件中并以灯箱样式显示/隐藏它。

回答by Ciaran Bruen

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:

不确定这是否正是您想要的,但您可以为此使用跨页发布。这允许您从子页面回发到父页面,并具有访问子页面的 ViewState 的优势。为此,您将按钮的 PostBackUrl 属性设置为子页面。在父页面中,您可以访问 PreviousPage 属性以从子页面检索值。所以在子页面中有一个按钮,例如:

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

Then in the Parent.aspx Page_Load event:

然后在 Parent.aspx Page_Load 事件中:

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

    }
}