【问题标题】:How to download image/pdf file from database using Gridview's linkbutton?如何使用 Gridview 的链接按钮从数据库下载图像/pdf 文件?
【发布时间】:2015-09-21 06:32:59
【问题描述】:

这是我的问题。我有一个用户控件,它将使用 gridview 中的链接按钮下载二进制文件(图像、pdf 等)。

<asp:GridView Visible="true" ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
AutoGenerateColumns="false">
<Columns>
            <asp:BoundField DataField="ID" HeaderText="ID"/>
    <asp:BoundField DataField="FileName" HeaderText="File Name"/>
    <asp:TemplateField HeaderText="Action" ItemStyle-HorizontalAlign = "Center">
        <ItemTemplate>
         <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile" CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
         <asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" OnClick="DeleteFile"  CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

代码背后:

Protected Sub DownloadFile(sender As Object, e As EventArgs)
    Dim id As Integer = Integer.Parse(TryCast(sender, LinkButton).CommandArgument)
    Dim bytes As Byte()
    Dim fileName As String, contentType As String

    Using con As New SqlConnection(DataSource.ConnectionString)
        Using cmd As New SqlCommand()
            cmd.CommandText = "select FileName, PatImage, FileType from DB where Id=@Id"
            cmd.Parameters.AddWithValue("@Id", id)
            cmd.Connection = con
            con.Open()
            Using sdr As SqlDataReader = cmd.ExecuteReader()
                sdr.Read()
                bytes = DirectCast(sdr("PatImage"), Byte())
                contentType = sdr("FileType").ToString()
                fileName = sdr("FileName").ToString()
            End Using
            con.Close()
        End Using
    End Using
    Response.Clear()
    Response.Buffer = True
    Response.Charset = ""
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.ContentType = contentType
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName)
    Response.BinaryWrite(bytes)
    Response.Flush()
    Response.End()
End Sub

但是,如果我的父 aspx 页面中有 UpdatePanel,这将不起作用。所以我用谷歌搜索并找到了答案;也就是在RowDataBound中放一段代码:

Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    Dim lb As LinkButton = TryCast(e.Row.FindControl("lnkDownload"), LinkButton )
    ScriptManager.GetCurrent(Me.Page).RegisterAsyncPostBackControl(lb)
End Sub

又出现了一个新错误。那就是我无法使用我的 RowDataBound 中的代码在我的 GridView 中找到我的链接按钮。

所以我再次用谷歌搜索,我发现我应该在我的 aspx 页面ClientIDMode="AutoID" 中添加一个属性。但这仅适用于框架 4.x。我不能这样做,因为我目前使用的是 3.5。我目前的情况有什么补救措施吗?

【问题讨论】:

  • 在呈现 GridView 控件之前,控件中的每一行都必须绑定到数据源中的一条记录。当数据行(由 GridViewRow 对象表示)绑定到 GridView 控件中的数据时,将引发 RowDataBound 事件。这使您能够提供一种事件处理方法来执行自定义例程,例如在发生此事件时修改绑定到行的数据的值。
  • 一个 GridViewRowEventArgs 对象被传递给事件处理方法,使您能够访问被绑定行的属性。要访问行中的特定单元格,请使用 GridViewRow 对象的 Cells 属性,该属性包含在 GridViewRowEventArgs 对象的 Row 属性中。您可以使用 RowType 属性确定正在绑定的行类型(标题行、数据行等)。
  • 我将首先用我的代码做什么?我对你的解释感到困惑。

标签: asp.net vb.net gridview asp.net-3.5 download


【解决方案1】:

已经找到答案了,我只是改了我的RowDataBound内容,参考代码:

Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim LButton = CType(e.Row.FindControl("lnkDownload"), System.Web.UI.WebControls.LinkButton)
        Dim scriptManager__1 = ToolkitScriptManager.GetCurrent(Me.Page)
        If scriptManager__1 IsNot Nothing Then
            scriptManager__1.RegisterPostBackControl(LButton)
        End If
    End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    相关资源
    最近更新 更多