Sumit Pannu
Sumit Pannu

Reputation: 177

Script Not working from code behind

I need to close the download window(opened using jquery framedialog) after clicking the download button inside the content page of the framedialog. I am creating a seperate iframe for sending download file in response like this:(Download.aspx sends the file to client based on file id)

var script = @"<script language=JavaScript>function Export(fileID)
                            {
                                    var iframe = document.createElement('iframe');
                                    iframe.src = 'DownloadForm.aspx?ID='+ fileID;
                                    iframe.style.display = 'none';
                                    document.body.appendChild(iframe);
                                    //alert('AlertBox'); 
                            }
                            </script>"; 
Page.ClientScript.RegisterStartupScript(typeof(string), "Startup", script);  

After completing download i am closing the download window. But my problem is that if i use the commented alertBox then my both codes works (download and close) fine. But i want it to happen without alert box. When i do not use alert box the Download.aspx page is not called. (I think the script is not going to client side while i register it and alert box is making it do something)..Dont know why its happening ..kindly help..Thanks in advance

Upvotes: 1

Views: 2157

Answers (4)

Sumit Pannu
Sumit Pannu

Reputation: 177

I found solution to my problem. Basically i was closing the download window before the downloading is completed at client's end. So, i added the frame for downloading(hidden) the file in the parent page of download window and then i close the download window

Upvotes: 1

smith269
smith269

Reputation: 135

Use this..

Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "function   name", true);

For your Case it will be.

Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "Export(file Id);", true);

Upvotes: 1

Aniket Inge
Aniket Inge

Reputation: 25705

You can instead, use:

ScriptManager.RegisterStartupScript(this, GetType(), "StartupScript", script, true);

Where script is:

var script = @"function Export(fileID)
                            {
                                    var iframe = document.createElement('iframe');
                                    iframe.src = 'DownloadForm.aspx?ID='+ fileID;
                                    iframe.style.display = 'none';
                                    document.body.appendChild(iframe);
                                    //alert('AlertBox'); 
                            }
                            //Export(YourFileIDHere);"; 

However, you should also note that the code only defines a function to be included in the script. You do not call this function anywhere in your JS code. (So I've included a commented code which can be uncommented for the function to execute, with proper fileID.

Upvotes: 1

Prashant16
Prashant16

Reputation: 1526

Try this

Page.ClientScript.RegisterStartupScript(getType(), "Startup", script, true); 

Upvotes: 1

Related Questions