Closing RadWindow by invoking javascript from server

kmkemp picture kmkemp · Oct 13, 2011 · Viewed 16.9k times · Source

I have a Control that looks like this:

<telerik:RadCodeBlock runat="server">
    <script type="text/javascript">
        function refresh() {
            window.$find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("RebindRecommendations");
        }
    </script>
</telerik:RadCodeBlock>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="AjaxRequest">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="NameOfGrid" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadWindowManager ID="RadWindowManager1" runat="server">
    <Windows>
        <telerik:RadWindow ID="RadWindow1" runat="server" NavigateUrl="UrlOfPage" OnClientClose="refresh"></telerik:RadWindow>
    </Windows>
</telerik:RadWindowManager>

Page:

<script type="text/javascript">
    function closeWindow() {
        self.close();
        return false;
    }
</script>
<!-- form fields here -->
<telerik:RadButton runat="server" ID="_cancel" Text="Cancel" OnClientClick="closeWindow"></telerik:RadButton>
<telerik:RadButton runat="server" ID="_submit" Text="Submit" OnClientClick="closeWindow" OnClick="DoSomeDataBaseStuff"></telerik:RadButton>

This works as intended. My RadWindow is removed and my control's grid refreshes upon hitting either the cancel or submit buttons. The problem is that the database isn't finished doing its work by that time so the grid refresh doesn't reveal the changes. I have attempted to switch my implementation of page to look like this:

Page:

<script type="text/javascript">
    function closeWindow() {
        self.close();
        return false;
    }
</script>
<!-- form fields here -->
<telerik:RadButton runat="server" ID="_cancel" Text="Cancel" OnClick="CallJavaScriptToKillWindow"></telerik:RadButton>
<telerik:RadButton runat="server" ID="_submit" Text="Submit" OnClick="DoSomeDataBaseStuffAndThenCallJavaScriptToKillWindow"></telerik:RadButton>

Code Behind eventually calls this after database work completes:

ClientScript.RegisterStartupScript(GetType(), "Key", "closeWindow();", true);

I hit my breakpoint in the closeWindow function, but it doesn't have the same behavior (the window doesn't actually close). I've tried various iterations like:

ClientScript.RegisterStartupScript(GetType(), "Key", "$(document).ready(function() {return closeWindow();});", true);

to no avail. What am I missing?

Answer

chrisdrobison picture chrisdrobison · Oct 13, 2011

What is 'self'? Don't you mean 'this'?

Are you trying to close the RadWindow from inside the window or from the page that launches the window?

From inside a window, I usually do this:

    function GetRadWindow()
    {
        var oWindow = null;
        if (window.radWindow)
            oWindow = window.radWindow;
        else if (window.frameElement.radWindow)
            oWindow = window.frameElement.radWindow;
        return oWindow;
    }

    function Close()
    {
        var oWindow = GetRadWindow();
        oWindow.argument = null;
        oWindow.close();
        return false;
    }

Also, I'd use ScriptManager.RegisterStartupScript rather than ClientScript.