为什么在ASP.Net中使用链接按钮在下载文件上抛出错误? [英] Why is it throwing error on Download file using link button in ASP.Net ?

查看:34
本文介绍了为什么在ASP.Net中使用链接按钮在下载文件上抛出错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Gridview中有一个链接按钮,点击它应该打开与之关联的文件。我得到的错误是

I have a link button in Gridview and clicking on it should open the file that is associated with it. The error I am getting is

JavaScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.







Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
      Try
          '  Dim grdrow as GridViewRow =DirectCast((LinkButton),sender.NamingContainer, GridViewRow)
          Dim filePath As String = CType(sender, LinkButton).CommandArgument
          If Not filePath = "" Then
              Response.ContentType = ContentType
              Response.AppendHeader("Content-Disposition", ("attachment; filename=" + Path.GetFileName(filePath)))
              Response.WriteFile(filePath)
              Response.OutputStream.Close()
              ' HttpContext.Current.Response.End()
          Else
              ScriptManager.RegisterStartupScript(Me, Me.GetType(), "script", "alert('No file attached');", True)
          End If
      Catch ex As Exception
          ScriptManager.RegisterStartupScript(Me, Me.GetType(), "script", "Error Downloading the File", True)
          LogError.LogErrorIntoTextFile(ex, "btnDownload_Click()")
      End Try
  End Sub





任何人都可以帮我解决这个错误吗?



can anyone help me in solving this error?

推荐答案

尝试添加scriptmanager(比如---> sm1)到页面或母版页(标记),

现在在网格代码隐藏处声明一个变量(sm),其中存在网格视图(ScriptManager sm;)



现在在gridview的行数据绑定事件中执行以下操作



Try adding the scriptmanager (say---> sm1)to the page or masterpage(markup),
now declare a variable(sm) in the webpage codebehind where the grid view is present( ScriptManager sm;)

Now in the row databound event of the gridview do the following

if (e.Row.RowType == DataControlRowType.DataRow)
{
sm = ScriptManager.GetCurrent(this.Page);
sm.RegisterPostBackControl(e.Row.FindControl("btnDownload"));
}







Note: I did it in C# you can try this vb and let me know if this works


这篇关于为什么在ASP.Net中使用链接按钮在下载文件上抛出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆