【问题标题】:Better way to download files that are checked in grid view下载在网格视图中检查的文件的更好方法
【发布时间】:2019-11-02 13:14:01
【问题描述】:

我有一个网格视图,其中包含一列复选框,然后是几列数据。选择一行时,将显示其相应的图像。任何行都可以在不选中的情况下进行检查,当单击下载按钮时,应下载所有检查了相应行的图像。现在我有一个 for 循环遍历每一行并找到复选框并查看它是否被检查,这需要太长时间。勾选复选框时有没有办法访问对应行的数据?

For Each row As GridViewRow In gv1.Rows
  Dim chkBox As CheckBox = TryCast(row.FindControl("cBox"), CheckBox)
  If chkBox.Checked Then
    Dim full = row.Cells(1).Text & "B" & row.Cells(3).Text
    IO.File.Copy(serv & full & ".TIF", Path.Combine(temp, full & ".TIF"), True)
  End If
Next

【问题讨论】:

  • 如果网格是数据绑定的,你不能检查数据吗?见GridView Class / Binding to Data
  • 可能实际的循环和复选框检查非常快。慢的部分就是所有的文件副本。
  • @OlivierJacot-Descombes 问题是在不遍历每一行的情况下找出选中的复选框在哪一行。
  • @EJC,我知道,但是如果您使用数据绑定,那么网格中的编辑将自动更新数据(例如,在 List<MyDataClass> 中)。因此,您可以循环列表并检查相应的bool 属性,而不是循环行、单元格和控件。你最终会选择单线:var selected = myList.Where(d => d.IsSelected); 或类似的东西。

标签: javascript css asp.net vb.net gridview


【解决方案1】:

这对我来说非常有效,非常有效。是 IO 副本花费了太长时间。

Using zip As New ZipFile()
        zip.AlternateEncodingUsage = ZipOption.AsNecessary
        zip.AddDirectoryByName("Drawings")
        For Each row As GridViewRow In gv1.Rows
            If TryCast(row.FindControl("cBox"), CheckBox).Checked Then
                found = True
                Dim filepath = serv & row.Cells(1).Text & "B" & row.Cells(3).Text & ".TIF"
                zip.AddFile(filepath, "Drawings")
            End If
        Next
        If found Then
            Response.Clear()
            Response.BufferOutput = False
            Dim zipname As String = [String].Format("Drawings_{0}.zip", DateTime.Now.ToString("yyyy-MM-dd-HH:mm:ss"))
            Response.ContentType = "application/zip"
            Response.AddHeader("content-disposition", "attachment; filename=" + zipname)
            zip.Save(Response.OutputStream)
            Response.End()
        Else
            reSetup()
        End If
    End Using

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多