Close RadWindow CodeBehind

4 posts, 0 answers
  1. Kevin
    Kevin avatar
    265 posts
    Member since:
    Jun 2011

    Posted 07 May 2014 Link to this post

    I have followed mutiple examples of Clsoing the Radwindow from the code behind, but nothing seems to be working. I have a couple applications where I have a need to do this but neither will close the radWindow and tehn refresh the existing page.  SO in this example, I am exporting to excel and then closing the Radwindow but it never closes.

    <script type="text/javascript">
            function getRadWindow() {
                var oWindow = null;
                if (window.radWindow)
                    oWindow = window.radWindow;
                else if (window.frameElement.radWindow)
                    oWindow = window.frameElement.radWindow;
                return oWindow;
            }
     
            // Reload parent page
            function refreshParentPage() {
                getRadWindow().BrowserWindow.document.location.href = url;
            }
     
    Or this one as well
     function refreshParentPage() {
                getRadWindow().BrowserWindow.location.reload();
            }
     
        </script>
     
     
    Here is the code behind, after the export I want the window to close but its a nogo.
     protected void lnkExport_Click(object sender, EventArgs e)
        {
            //Get the Admins that are checked from the dropdown
            String checkId = string.Empty;
            foreach (RadComboBoxItem checkeditem in cbAdmins.CheckedItems)
            {
                 checkId += checkeditem.Value + ',';
            }
     
            checkId = checkId.TrimEnd(' ', ',');
     
            if (checkId == string.Empty)
            {
                ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "OpenWindow", "alert('Need to pick an Administrator. If none are able to be picked please assign via admin page.');", true);
            }
            else
            {
                //Create the Excel File and ready it to be written and shown.
                var ExistFile = Server.MapPath("~/Excel/NSS.xlsx");
                var File = new FileInfo(ExistFile);
     
                using (ExcelPackage package = new ExcelPackage(File))
                {
                    ExcelWorksheet workSheet = package.Workbook.Worksheets["Sheet1"];
     
                    //get all the admins that where Checked but only allow 2
                    sql = "Select TOP 2 Name, Mail, strFtPhone, strAddress from vw_tblAdmins where intAdminId in (" + checkId + ")";
     
                    dt = new DataTable();
                    dt = c.GetReader(sql);
     
                    //1st Row starts on A8 to E8
                    int rowC = 8;
                    foreach (DataRow row in dt.Rows)
                    {
                        workSheet.Cells["A" + rowC].Value = row[0].ToString();
                        workSheet.Cells["B" + rowC].Value = "MN-ARNG";
                        workSheet.Cells["C" + rowC].Value = row[1].ToString();
                        workSheet.Cells["D" + rowC].Value = row[2].ToString();
                        workSheet.Cells["E" + rowC].Value = row[3].ToString();
                        rowC += 1;
                    }
     
                    //Get all the Exportables for the SpreadSheet
                    sql = "Select Name, Persona, Email, Phone, Address, intRequestId from vw_TokenExport where dtOrdered IS NULL and location = " + cbLocations.SelectedValue;
     
                    dt = new DataTable();
                    dt = c.GetReader(sql);
     
                    //1st Row starts on A16 to E16
                    string requestId = string.Empty;
                    int user = c.GetUserId();
                    int rowCount = 16;
                    foreach (DataRow row in dt.Rows)
                    {
                        workSheet.Cells["A" + rowCount].Value = row[0].ToString();
                        workSheet.Cells["B" + rowCount].Value = row[1].ToString();
                        workSheet.Cells["C" + rowCount].Value = row[2].ToString();
                        workSheet.Cells["D" + rowCount].Value = row[3].ToString();
                        workSheet.Cells["E" + rowCount].Value = row[4].ToString();
                        requestId += row[5].ToString() + ',';
                        rowCount += 1;
                    }
     
                    //take of the end Comma
                    requestId = requestId.TrimEnd(' ', ',');
     
                    //Update the table with the dtOrdered and by Whom
                    sql = "Update tblTokenRequest set dtOrdered = '" + DateTime.Today + "', intOrderedBy = " + user + " where intRequestId in (" + requestId + ")";
     
                    c.InsertUpdateDelete(sql);
     
                    Response.Clear();
                    Response.ContentType = "application/vnd.ms-excel";
     
                    Response.AddHeader("Content-Disposition", "attachment; filename=NSSRequest" + DateTime.Today + ".xlsx");
                    Response.BinaryWrite(package.GetAsByteArray());
                    Response.Flush();
                    Response.Close();
     
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "CloseScript", "redirectParentPage('ManageRequests.aspx')", true);
     
    or this one as well
     Page.ClientScript.RegisterClientScriptBlock(GetType(),"CloseScript", "refreshParentPage()", true);
                }
            }
        }







































































  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 08 May 2014 in reply to Kevin Link to this post

    Hi Kevin,

    Please try the following sample code snippet to Close the RadWindow from code behind.

    ASPX:
    <asp:Button ID="OpenButton" runat="server" Text="OpenWin" OnClick="OpenButton_Click" />
    <telerik:RadWindow ID="radwinExportExcel" runat="server">
        <ContentTemplate>
            <asp:Button runat="server" Text="Close" ID="CloseButton" OnClick="CloseButton_Click" />
        </ContentTemplate>
    </telerik:RadWindow>

    C#:
    protected void CloseButton_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "close", "CloseModal();", true);
    }
    protected void OpenButton_Click(object sender, EventArgs e)
    {
        radwinExportExcel.VisibleOnPageLoad = true;
    }

    JavaScript:
    <script type="text/javascript">
        function GetRadWindow() {
            var oWindow = null;
            if (window.radWindow)
                oWindow = window.radWindow;
            else if (window.frameElement && window.frameElement.radWindow)
                oWindow = window.frameElement.radWindow;
            return oWindow;
        }
        function CloseModal() {
            setTimeout(function () {
                GetRadWindow().close();
            }, 0);
        }
    </script>

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kevin
    Kevin avatar
    265 posts
    Member since:
    Jun 2011

    Posted 08 May 2014 in reply to Shinu Link to this post

    Hi Shinu,

    Ok I copied as such but it still does not close the radwindow when it does the Excel export, it seems as the excel export to another window messes with the window close. I also have another issue in that the radwindow postsback to the page on my required postsback and makes the page blink even though I have it in an Ajax panel.

    <telerik:RadWindow ID="rwExportExcel" runat="server"  Width="400px" Height="200px" Behaviors="Close" CenterIfModal="true" Modal="true" ShowContentDuringLoad="false" VisibleStatusbar="false" ReloadOnShow="false" Skin="Web20">
                    <ContentTemplate>
                        <div class="CentDiv">
                            <telerik:RadComboBox ID="cbLocations" runat="server" Width="300px" ExpandDirection="Down" Skin="Web20" OnDataBound="cbLocations_DataBound" AutoPostBack="true" OnSelectedIndexChanged="cbLocations_SelectedIndexChanged"></telerik:RadComboBox>
                            <br />
                            <br />
                            <telerik:RadComboBox ID="cbAdmins" runat="server" Width="300px" CheckBoxes="true" ExpandDirection="Down" Skin="Web20" Visible="false"></telerik:RadComboBox>
                            <br />
                            <br />
                            <asp:Button runat="server" Text="Close" ID="CloseButton" OnClick="CloseButton_Click" />
                        </div>
                    </ContentTemplate>
                </telerik:RadWindow>
     
    This is in the content template of the Radgrid to open the window.
     <CommandItemTemplate>
                            <asp:Button ID="OpenButton" runat="server" Text="OpenWin" OnClick="OpenButton_Click" /
       </CommandItemTemplate>
     
    OpenButton
     protected void OpenButton_Click(object sender, EventArgs e)
        {
            rwExportExcel.VisibleOnPageLoad = true;
            FillDll();
        }
     
    Close button  does nto matter where I put the close, does not close it, is there a way around.
     
     protected void CloseButton_Click(object sender, EventArgs e)
        {
            //Get the Admins that are checked from the dropdown
            String checkId = string.Empty;
            foreach (RadComboBoxItem checkeditem in cbAdmins.CheckedItems)
            {
                checkId += checkeditem.Value + ',';
            }
     
            checkId = checkId.TrimEnd(' ', ',');
     
            if (checkId == string.Empty)
            {
                ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "OpenWindow", "alert('Need to pick an Administrator. If none are able to be picked please assign via admin page.');", true);
            }
            else
            {
                
                //Create the Excel File and ready it to be written and shown.
                var ExistFile = Server.MapPath("~/Excel/NSS.xlsx");
                var File = new FileInfo(ExistFile);
     
                using (ExcelPackage package = new ExcelPackage(File))
                {
                    ExcelWorksheet workSheet = package.Workbook.Worksheets["Sheet1"];
     
                    //get all the admins that where Checked but only allow 2
                    sql = "Select TOP 2 Name, Mail, strFtPhone, strAddress from vw_tblAdmins where intAdminId in (" + checkId + ")";
     
                    dt = new DataTable();
                    dt = c.GetReader(sql);
     
                    //1st Row starts on A8 to E8
                    int rowC = 8;
                    foreach (DataRow row in dt.Rows)
                    {
                        workSheet.Cells["A" + rowC].Value = row[0].ToString();
                        workSheet.Cells["B" + rowC].Value = "MN-ARNG";
                        workSheet.Cells["C" + rowC].Value = row[1].ToString();
                        workSheet.Cells["D" + rowC].Value = row[2].ToString();
                        workSheet.Cells["E" + rowC].Value = row[3].ToString();
                        rowC += 1;
                    }
     
                    //Get all the Exportables for the SpreadSheet
                    sql = "Select Name, Persona, Email, Phone, Address, intRequestId from vw_TokenExport where dtOrdered IS NULL and location = " + cbLocations.SelectedValue;
     
                    dt = new DataTable();
                    dt = c.GetReader(sql);
     
                    //1st Row starts on A16 to E16
                    string requestId = string.Empty;
                    int user = c.GetUserId();
                    int rowCount = 16;
                    foreach (DataRow row in dt.Rows)
                    {
                        workSheet.Cells["A" + rowCount].Value = row[0].ToString();
                        workSheet.Cells["B" + rowCount].Value = row[1].ToString();
                        workSheet.Cells["C" + rowCount].Value = row[2].ToString();
                        workSheet.Cells["D" + rowCount].Value = row[3].ToString();
                        workSheet.Cells["E" + rowCount].Value = row[4].ToString();
                        requestId += row[5].ToString() + ',';
                        rowCount += 1;
                    }
     
                    //take of the end Comma
                    requestId = requestId.TrimEnd(' ', ',');
     
                    //Update the table with the dtOrdered and by Whom
                    sql = "Update tblTokenRequest set dtOrdered = '" + DateTime.Today + "', intOrderedBy = " + user + " where intRequestId in (" + requestId + ")";
     
                    c.InsertUpdateDelete(sql);
     
                    Response.Clear();
                    Response.ContentType = "application/vnd.ms-excel";
     
                    Response.AddHeader("Content-Disposition", "attachment; filename=NSSRequest" + DateTime.Today + ".xlsx");
                    Response.BinaryWrite(package.GetAsByteArray());
                    Response.Flush();
                    Response.Close();
     
                    ScriptManager.RegisterStartupScript(this, GetType(), "close", "CloseModal();", true);
                }
            }
        }



















  5. Marin Bratanov
    Admin
    Marin Bratanov avatar
    3595 posts

    Posted 09 May 2014 Link to this post

    Hello Kevin,

    Closing a RadWindow (or any other interaction with the page) is not possible when you send a file instead of the Page. Here is what happens:

    1. the buttons POSTs the page
    2. The script to close the dialog is registered and added to the Response the Page will send (markup, scripts ,etc.)
    3. The file is written to the response stream and it replaces the standard page response, so no markup or scripts travel to the client

    Thus, you cannot change anything on the page, even a simple browser alert() will not execute.

    On the way AJAX should work in this setup - please examine this article on using a RadWindow with AJAX: http://www.telerik.com/help/aspnet-ajax/radwindow-ajaxifying.html. Instead of wrapping the entire control in a RadAjaxPanel, you need to wrap its contents. Thus, they will perform partial postbacks. The problem with this is that you cannot send a file during a partial postback.

    I would also advise that you employ this approach for opening the RadWindow from the server: http://www.telerik.com/help/aspnet-ajax/window-troubleshooting-opening-from-server.html.

    Perhaps an option to have the best of both worlds (partial postbacks and the RadWindow closing + the user getting the file) would be to:

    1. create a JavaScript function that will close the RadWindow
    2. call this function from the code-behind
    3. store the file somewhere (e.g., in the session or in the file system)
    4. after closing the RadWindow, point the browser to this file (or to some handler that will send the file) through JavaScript, as suggested here: http://www.telerik.com/help/aspnet-ajax/ajax-download.html.

    You could call the funcitons in the same RegisterStartupScript() call, this is not really relevant. You could pass the file URL as an argument and use only one function, the exact code is more a matter of refactoring.

    I hope this explains the case and lets you choose the approach most appropriate for you.


    Regards,

    Marin Bratanov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top