简体   繁体   English

服务器端重定向后jQuery对话框不起作用

[英]jQuery dialog not working after server-side redirect

I have an ASP.NET website which uses the jQuery dialog to present the user with a downloads terms dialog. 我有一个ASP.NET网站,该网站使用jQuery对话框向用户显示下载条件对话框。 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. 用户同意对话框中的条款后,我将执行服务器端回发,发出对另一个网站上负责下载到浏览器的页面的Response.Redirect调用。 The problem is that once the Response.Redirect call has been made, the dialog can no longer be shown. 问题在于,一旦进行了Response.Redirect调用,就无法再显示该对话框。

I initialize the dialog in the document.ready event using the following code :- 我使用以下代码在document.ready事件中初始化对话框:

$("#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. 此代码使我能够成功多次显示对话框,但是在进行Response.Redirect调用后,对dialog(“ open”)的调用将不再起作用。

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. 另外,如果任何人都可以提供任何jQuery对话框调试技巧,也将不胜感激。

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. 问题似乎是由我的对话框使用的扩展功能引起的,该功能将jQuery对话框放置在页面的表单部分中,以确保服务器端的回发将正常工作。 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, "") %>;

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. 不幸的是,我无法说明任何可能的原因,但是在您的位置,我尝试使用控制台和断点之类的Firebug工具从一些调试代码开始。 Maybe some kind of var initalization or similar error happens. 也许某种形式的var initalization或类似的错误发生。 Hope this tip will help you a bit. 希望本技巧对您有所帮助。

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. 考虑到您确实重定向了用户,并且在该重定向中您将重定向到上一页(具有对话框的页面),因此(document).ready再次启动并初始化对话框。

But, using ASP.NET you should not do this like you are doing, think about a simple form, to you and the user. 但是,使用ASP.NET时,您不应该像您正在做的那样,而是为您和用户考虑一个简单的表单。

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: 在jQuery中使用.load可以加载不同的页面,因此让我们假设您在对话框中有一个名为“我同意”的按钮,该按钮向用户提供了所需的文件:

$(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: 在您的getFile.aspx中,删除除第一行以外的所有HTML代码,然后在Page_Load事件背后的代码中处理请求,并使用Response.Write方法将文档写回,例如:

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. 请记住在请求中创建一个cookie,并在aspx页面中检查该cookie(如果不存在),这是因为用户强行下载了该cookie,然后在将文件发送回用户之前删除了该cookie。

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. Cookie只是确保可信度的一种策略,但这并不是完美无缺的,因为您需要用户启用Cookie(例如Hotmail才能使您在其网站上阅读电子邮件),您可以使用Session对象或其他您认为可以使用的方法关于。

This is only so you can have a better idea of what you can accomplish. 只有这样,您才能更好地了解自己可以完成的工作。

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. 当我们从页面发送请求时,jquery脚本成功执行,但是当我们在发送重新查询的页面上进行响应时,jquery脚本未执行。 this is the reason ur pop up not displaying 这是您的弹出窗口不显示的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM