开发者

Is it possible to call a server-side event on a parent page when a child page closes?

I have a parent page with two data controls. I want to be able to open a child window, do something on it, and when it closes I want to rebind only one of the two data controls on the parent page. I have the control I want to update within an UpdatePanel so would like to call rebind it and call UpdateP开发者_Go百科anel.Update().


From a child window, you can't actually "call" a server side function for the parent page, however you can use some javascript to invoke client side functions on that page.

On the parent page:

<script language="Javascript" type="text/javascript">
function CallAlert()
{
    alert("This is parent window's alert function.");
}
</script>

On the child page:

<script language="Javascript" type="text/javascript">
function CallParentWindowAlert()
{
    window.opener.CallAlert();
    return false;
}
</script>

In the example you provided, in particular where you have an UpdatePanel, you've actually left yourself a few options here. If the UpdatePanel is set with some form of trigger to force it to update, be it all children or just specified ones, in your parent function you can force a postback on one of those controls. Ultimately, the parent pages javascript function should have some form of a __doPostBack() call, referencing the id of a control and some (empty) parameter, however you'd probably be better off generating that javascript with codebehind via:

Page.ClientScript.GetPostBackEventReference(control, null);

Once you put that together, it's all a matter of tying the actual invocation to whatever client side event you want on your child page, be it onUnLoad() or in some custom function you call.


Try this out all you got to do is call __doPostBack on the parent page targeting the update panel or something in the update panel.

Parent Code:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Parent Window</h1>
        Time:
        <%= DateTime.Now.ToString() %>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                Update Panel Time<%= DateTime.Now.ToString() %>
                <br />
                <asp:Button ID="Button1" Text="Submit" runat="server" />
                <a href="#" onclick="window.open('child.aspx')">Click To Open Child Window</a>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

Child Code:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript" language="javascript">
        function UnloadMe() {
            if (opener) {
                opener.__doPostBack("UpdatePanel1", "");
            }
        }
    </script>

</head>
<body onunload="UnloadMe()">
    <form id="form1" runat="server">
    <div>
    <h1>Child Window</h1>
    </div>
    </form>
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜