【解決方法】ASP.Net のリンク ボタンを使用してファイルをダウンロードするとエラーが発生するのはなぜですか?

プログラミングQA


Gridview にリンク ボタンがあり、それをクリックすると、関連付けられているファイルが開きます。 私が得ているエラーは

JavaScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.
VB
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

誰でもこのエラーを解決するのを手伝ってもらえますか?

解決策 1

ページまたはマスターページ (マークアップ) に scriptmanager (たとえば —> sm1) を追加してみてください。
グリッド ビューが存在する Web ページのコード ビハインドで変数 (sm) を宣言するようになりました ( ScriptManager sm;)

グリッドビューの行データバインド イベントで、次の操作を行います。

C#
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

コメント

タイトルとURLをコピーしました