c# - asyncfileupload 上传后显示图像而不刷新页面

标签 c# asp.net ajax

我一直在努力解决这个问题,但没有运气。

使用 asyncfileupload 控件上传文件并显示图像。上传工作正常,如果我重新加载/刷新页面,则会显示图像。

但需要知道如何在不重新加载/刷新页面的情况下执行此操作。

阅读在线帖子后,我看到了使用 scriptmanager 的建议,但这对我不起作用:

    protected void FileUploadComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {

          .
          .
          .
          .

        ScriptManager.RegisterStartupScript(this, GetType(), "TestAlert",
        "window.parent.document.getElementById('" + img_ProfilePic.ClientID + "').src='" + "http://www.site.com/default.jpg" + "');",
        true); 

谢谢, 贝鲁兹

最佳答案

在发现 user554134 提到的错误之前浪费了几个小时,我想出了以下使用 jQuery 的解决方案(以及 asyncfileupload 控件)。

  • 首先得到编译好的版本 修复了错误的 AjaxToolkit, 每个 user554134 的链接。

  • 添加一些 jQuery 来隐藏/显示图像。

$(document).ready(function () {
            $("#imgConfirmation").hide();
        });

要记住的关键点是 AsyncFileUpload 控件使用 iframe,因此在调用“显示”函数时需要让 jquery 访问框架的父级。例如:

        function showImage(imagePath) {
            $('#imgConfirmation', window.parent.document).attr("src", imagePath);
            $('#imgConfirmation', window.parent.document).show();
        }
  • 在您的 UploadedComplete 事件中,保存文件后,注册 挂接到 js 函数的客户端脚本。例如,
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "showImage", "showImage('" + myLink + "');", true);

关于c# - asyncfileupload 上传后显示图像而不刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4721114/

相关文章:

javascript - 未经确认不得删除对象

javascript - 在 Rails 中通过轮询刷新多个部分

c# - 使用 ProcessStartInfo 作为 LocalSystem 启动进程

c# - 为什么 Int64 和 UInt64 大小相同

c# - 什么时候值得压缩 ViewState?

c# - 当负载均衡器不是 "sticky"时,如何在 MVC 的 Session 中保存数据?

javascript - 从 AngularJS $http get 返回对象

c# - 从 MethodCallExpression 访问调用对象

c# - C# WinForm static void Main 不能捕获异常吗?

c# - 循环遍历通过 FileUpload 控件上传的 txt 文件行