locked
Unable to invoke JavaScript function with RegisterStartupScript while downloading a file RRS feed

  • Question

  • User1256154619 posted

    I am unable to invoke a java script function in the aspx upon a successful file download

    Here is my aspx

        <asp:GridView id="gvOutboundResults" runat="server" Visible="true" GridLines="Horizontal" BorderWidth="0" ShowHeaderWhenEmpty="true" EnableViewState="true" 
            AutoGenerateColumns="false" OnRowCommand="btnDownloadFile_Click" CssClass="col-md-12">
            <EmptyDataRowStyle BorderWidth="0" BorderStyle="None" HorizontalAlign="Center" BorderColor="White" />
            <EmptyDataTemplate>
                No Downloads Available</EmptyDataTemplate>
            <RowStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="Erow"/>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#69E2EF" />
            <RowStyle BackColor="white"/>
            <PagerStyle ForeColor="#2461BF" BackColor="White" HorizontalAlign="Left" />
            <HeaderStyle ForeColor="Black" BackColor="White" Font-Size="Medium" Font-Names="Arial" BorderWidth="0" />
            <AlternatingRowStyle BackColor="#f1f1f1" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Left" />
            <Columns>        
                <asp:TemplateField HeaderStyle-Width="300px" HeaderText="Download File" HeaderStyle-Font-Size="13px">
                    <ItemTemplate>
                        <asp:ImageButton ID="imgDownload" runat="server"  
                            ImageUrl="~/images/download.ico" ToolTip="Download File"
                            CommandArgument='<%# Eval("File_Name") %>' CausesValidation="false" CommandName="Download" OnClientClick="StartDownload(this)" />
                        <div id="imgDownloading" runat="server" style="display:none"><span class="ld ld-ring ld-spin"></span></div>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

    Here is my code behind for the btnDownloadFile_Click event where I tried every version of RegisterStartupScript call, with script tags, without, with semicolons, without, etc

    protected void btnDownloadFile_Click(object sender, GridViewCommandEventArgs e)
    {
    
    
        try
        {
    
    
            lblMessage.Text = string.Empty;
            lblMessage.Visible = false;
    
    
            if (e.CommandName == "Download")
            {
                string fileName = e.CommandArgument.ToString();
                string filePath = myLocation;
                drms_utilities.DownloadFile(this, filePath, lblMessage);
            }
        }
        catch(Exception exception)
        {
            lblMessage.Text = exception.Message;
            lblMessage.Visible = true;
            drmsda.InsertErrorlog(pageName, "External: Error Occtured in btnDownloadFile_Click Event.", exception.Source, exception.Message + " " + exception.StackTrace, "");
        }
        finally
        {
            String csname1 = "PopupScript";
            Type cstype = this.GetType();
            // Get a ClientScriptManager reference from the Page class.
            ClientScriptManager cs = Page.ClientScript;
    
    
            // Check to see if the startup script is already registered.
            if (!cs.IsStartupScriptRegistered(cstype, csname1))
            {
                string function = "FinishDownload('" + imageButton.ClientID + "')";
                cs.RegisterStartupScript(cstype, csname1, "alert('anton');", false);
            }
    
    
            ScriptManager sm = ScriptManager.GetCurrent(Page);
            if (sm.IsInAsyncPostBack)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "anotherKey", "someAlert();", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "anotherKey", "Sys.Application.add_load(someAlert);", true);
            }
    
    
            Page.ClientScript.RegisterStartupScript(this.GetType(), "anotherKey", "Sys.Application.add_load(someAlert)", true);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "anotherKey", "someAlert();", true);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "anotherKey", "someAlert()", true);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "anotherKey", "<script language='javascript'>someAlert()</script>", true);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "anotherKey", "<script language='javascript'>someAlert();</script>", true);
    
    
            ScriptManager.RegisterStartupScript(this, this.GetType(), "anotherKey", "Sys.Application.add_load(someAlert)", true);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "anotherKey", "someAlert();", true);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "anotherKey", "someAlert()", true);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "anotherKey", "<script language='javascript'>someAlert()</script>", true);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "anotherKey", "<script language='javascript'>someAlert();</script>", true);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "anotherKey", "Sys.Application.add_load(someAlert)", true);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "anotherKey", "someAlert();", true);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "anotherKey", "someAlert()", true);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "anotherKey", "<script language='javascript'>someAlert()</script>", true);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "anotherKey", "<script language='javascript'>someAlert();</script>", true);
    
    
        }
    }

    Function call for DownloadFile contains the following code

                var ZipFile = new FileInfo(archivePath);
                archiveName = Path.GetFileName(archivePath);
                page.Response.Clear();
                page.Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", archiveName));
                page.Response.ContentType = "application/x-zip-compressed";
                page.Response.WriteFile(ZipFile.FullName);
                page.Response.End();

    Just in case here is my JavaScript function

    function someAlert()
    {
        alert('Hi I am registered');
    }
    Monday, December 7, 2020 5:19 PM

All replies