【问题标题】:asp.net C# how to do Response.Redirect inside taskasp.net C#怎么做Response.Redirect里面的任务
【发布时间】:2018-06-30 02:33:53
【问题描述】:

这是我的代码。

protected void Send(object sender, ImageClickEventArgs e)
    {
        try
        {

            ObjCancellationTokenSource = new CancellationTokenSource();
            CancellationToken token = ObjCancellationTokenSource.Token;
            var ObjTask = Task<string>.Factory.StartNew(() =>
               {
                  while (!token.IsCancellationRequested)
                   {
                      for(i=0;i<n;i++)
                       { 
                         ----------some operation------ 
                       }
                     Response.Write("<script>alert('Completed.');</script>"); // not firing
                     Response.Redirect("Home.aspx", false);// raising error or not firing
                  }
               }, token);

        catch (Exception ex)
        {

        }
    }

所以,我的问题是……

一旦在任务中完成循环,我将尝试通过 Response.Write 显示警报,并且在我需要通过 Response.Redirect 将我的页面重定向到另一个页面之后...

但两者都不起作用......

我怎样才能实现它??

如果我在不使用任务操作的情况下同时执行 Response.Redirect 和 Response.Write.. 它工作正常。

我用谷歌搜索了这个 .. 但在所有站点中,都使用 Console.Write 方法来显示一些警报。我不想要那个.. 我需要显示真正的 javascript 警报并重定向页面....

请不要提供任何网络参考..提供一些代码...

谢谢大家。

【问题讨论】:

  • 为什么循环中有Response.Redirect?你想重定向多少次?
  • @Hooman .. 上面不是实际代码.. 我的问题在这里.. 是否可以在任务循环中使用 responce.redirect 和 responce.write .. 我需要将控件重定向到另一个任务或内部循环完成后的页面..
  • 这可能会有所帮助:stackoverflow.com/questions/21036805/…
  • @Hooman .. 给定的链接很有用.. 但是我使用 .net 4.0 开发的 Web 应用程序 .. “RedirectToAction”在 4.0 中不起作用...还有其他方式吗??

标签: c# asp.net cancellationtokensource


【解决方案1】:

请试试这个。 Response.Write("alert('Completed.');");替换此代码 ScriptManager.RegisterClientScriptBlock(this, GetType(), "SUCEESSFULL", "alert('Completed');", true); 在 Response.Redirect 处理此代码之后...

【讨论】:

  • 我也试过 ScriptManager.RegisterClientScriptBlock.. 但没有用.. 不工作.. 完全关键字“响应”在任务操作中不起作用..
  • 如果我使用“HttpContext.Current.Response.Redirect("UserPage.aspx", true);".. NullReferenceException 被用户代码取消处理..错误正在引发..
  • 您使用任何更新面板或脚本管理器??
  • 是的..我正在使用..上面给出的代码是在imagebutton点击事件中编写的..所以通过使用更新面板,我运行.gif图像直到任务循环完成..
  • 尝试此代码后删除更新面板.....可能正在使用此代码...ScriptManager.RegisterClientScriptBlock(this, GetType(), "SUCEESSFULL", "alert('Completed'); ", 真);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
  • 2010-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多