views:

2591

answers:

4

I have an ASP.NET website which uses the jQuery dialog to present the user with a downloads terms dialog. Once the user has agreed to the terms in the dialog, the I perform a server-side postback which issues a Response.Redirect call to a page on another website which is responsible for serving the download to the browser. The problem is that once the Response.Redirect call has been made, the dialog can no longer be shown.

I initialize the dialog in the document.ready event using the following code :-

$("#terms-dialog").dialog({
  modal: true,
  autoOpen: false,
  autoResize: false,
  height: 420,
  width: 500,
  overlay: {
    opacity: 0.5,
    background: "black"
  }
});

The code to show the dialog is as follows :-

function showTermsDialog(snippetid, title, agreement, url)
{
  $("#terms-dialog-text").html(agreement);
  $("#terms-dialog-controls").attr("style", "display: block;");
  $("#<%= this.SnippetID.ClientID %>").attr("value", snippetid);
  $("#<%= this.DownloadUrl.ClientID %>").attr("value", url);
  $("#terms-dialog").data("title.dialog", title); 
  $("#terms-dialog").dialog("open");
}

This code allows me to successfully show the dialog multiple times, but the call to dialog("open") no longer works after the Response.Redirect call has been made.

Has anyone done anything similar to what I am attempting to do here? Alternatively, if anyone can offer any jQuery dialog debugging tips, these would also be appreciated.

A: 

Unfortunately, I can't say anything about possible reasons, but on your place I've tried to start with some debugging code using such Firebug tools as console and breakpoints. Maybe some kind of var initalization or similar error happens. Hope this tip will help you a bit.

Sergii
A: 

there are a couple of things that you are probably doing wrong.

First of all the initialization of the dialog, qhere did you puted it? in

$(document).ready( function() { ... } );

if not, you should. Taking that you do redirect the user and in that redirect you redirect to the previous page (the one that have the dialog) so (document).ready is fired up again and initializes the dialog.

But, using ASP.NET you should not do this like you are doing, think about a simple form, to you and the user.

using .load in jQuery you can load a diferent page, so let's imagine that you have a button in the dialog called "I agree" that gives the user the requested file:

$(document).ready( function() {
  $("#terms-dialog").dialog({
    modal: true,
    autoOpen: false,
    autoResize: false,
    height: 420,
    width: 500,
    overlay: {
      opacity: 0.5,
      background: "black"
    },
    buttons: {
      I do not agree: function() { $(this).dialog('close'); }
      I agree: function() { 
        GetFileIfCookieExist("myfile.xls");
        $(this).dialog('close'); 
      }
    }
  });
});

function GetFileIfCookieExist(file) {
  // create cookie here
  $.load("getFile.aspx", { "f": file }, function() {
    // you can show a "loading" image
  });
}

in your getFile.aspx, delete all HTML code except 1st line and in the code-behind Page_Load event process the request and write the document back using the Response.Write method like:

Response.Clear(); 
Response.AddHeader("content-disposition", "attachment;filename=" + Request["f"]);
Response.Charset = "";
Response.ContentType = "application/vnd.xls";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
stringWrite = AppendMyFile();

Response.Write(stringWrite.ToString());
Response.End();

remember to create a cookie in the request and check that cookie in the aspx page, if it does not exist that's because a user forced it's download, and then delete the cookie before sending the file back to the user.

the cookie is only a strategy to assure credebility, but it's not flawless cause you need the user to have cookies enable (like Hotmail does in order you to read email in it's website), you can use Session object or other method that you can think about.

This is only so you can have a better idea of what you can accomplish.

balexandre
I do initialize the dialog in $(document).ready and serve the content in a similar manner to what you suggest, but the user isn't redirected back once the download has finished, so $(document).ready doesn't fire again. I do have logic that ensures the dialog is initialized again, but it doesn't help
Cleggy
are you working with an UpdatePanel? if yes, maybe you can use a simple trick that I answer here: http://stackoverflow.com/questions/536605/jquery-ui-simple-example-what-am-i-doing-wrong/536967#536967
balexandre
+1  A: 

The problem seemed to be caused by an extension function my dialog was using which was positioning the jQuery dialog into the form section of the page, to ensure server-side postbacks would work correctly. I changed the behaviour of this code to instead simply close the dialog and then invoke the serverside callback manually using the following

<%= this.Page.ClientScript.GetPostBackEventReference(btnAgree, "") %>;
Cleggy
A: 

when we send a request from a page the jquery scripts executed successfully but when we take response on same page through which we sent the requery the jquery scripts does not executed. this is the reason ur pop up not displaying