0

我使用 for 循环条件创建了一个动态表。其中有一个按钮,当我单击特定按钮时它应该打开一个文件。但在我的编码中,它在最后一行打开一个文件按钮。

 If Not IsPostBack Then
      txtlogsdate.Text = FormatDate(Now)
 End If

 Try
     trViewlogs.Visible = True
     lbllogs.Visible = False
     lbllogname.Visible = True
     RT1.Visible = False
     pb.Visible = False

     Dim d1 As DateTime = txtlogsdate.Text
     Dim dd As String = d1.ToString("dd")
     Dim mm As String = d1.ToString("MM")
     Dim yy As String = d1.ToString("yy")
     Dim d2 As String = yy & "" & mm & "" & dd

     Dim di As DirectoryInfo = New DirectoryInfo(Server.MapPath("~\logs"))
     Dim files As FileInfo() = di.GetFiles("*.log")

     Dim tab As New Table()
     tab.CellPadding = 0
     tab.CellSpacing = 0
     tab.BorderStyle = BorderStyle.Double
     tab.Attributes.Add("style", "margin-left: 0.5px; width: 800px;")

     Dim row As New TableRow()                
     Dim headerCell1 As New TableHeaderCell()
     headerCell1.Text = "Logs"

     headerCell1.Attributes.Add("style", "margin-left: 0.5px; height: 20px;")
     headerCell1.BackColor = System.Drawing.Color.CornflowerBlue
     headerCell1.ForeColor = System.Drawing.Color.White
     row.Controls.Add(headerCell1)
     tab.Controls.Add(row)


     Dim headerCell2 = New TableHeaderCell()

     headerCell2.Attributes.Add("style", "margin-left: 0.5px; height: 20px;")
     headerCell2.BackColor = System.Drawing.Color.CornflowerBlue
     headerCell2.ForeColor = System.Drawing.Color.White
     headerCell2.Text = "Download"
     row.Controls.Add(headerCell2)
     tab.Controls.Add(row)

     For i As Integer = 0 To files.Length - 1
         Dim a As String = files(i).ToString.Replace("Event-", "")
         Dim c As String = a.Substring(0, 6)
         Dim sw As String

         If d2 = c Then
             sw = My.Computer.FileSystem.ReadAllText(_
               GetWebSitePhysicalRoot & "\logs\" & files(i).ToString)

             lbllogname.Text = files(i).ToString                     
             lbllogname.Visible = False

             row = New TableRow()

             If i Mod 2 = 0 Then
                 row.BackColor = System.Drawing.Color.White
             Else
                 row.BackColor = System.Drawing.Color.AliceBlue
             End If
             Dim cell As New TableCell()
             cell.Text = lbllogname.Text
             'cell.Width = New Unit("1000px")
             cell.HorizontalAlign = HorizontalAlign.Center
             row.Controls.Add(cell)

             Dim cell2 As New TableCell()                              

             Dim bt As New Button
             bt.BorderStyle = BorderStyle.Solid

             bt.Text = files(i).ToString

             AddHandler bt.Click, AddressOf bt_Click

             cell2.HorizontalAlign = HorizontalAlign.Center
             cell2.Controls.Add(bt)
             row.Controls.Add(cell2)
             tab.Controls.Add(row)
             Panel1.Controls.Add(tab)
         End If

     Next i
     If lbllogname.Text = "" Then
         lbllogname.Text = "No Logs to Display !"
     End If

     Session("pageurl") = ""
     Session("pagecount") = ""
     ib.SetInfo("Reports > View Logs", Infobar.InfoTypes.Caption)
     Dim mi As Integer = GetQueryStringToInt("menuindex", 1)
     If Not IsPostBack Then
         leftmenu1.AddItem("View Logs", _
         GetWebSiteUrlRoot & "/staff_rpt.aspx?rpt=logs&page=1&menuindex=" & mi)
     End If

  Catch ex As Exception
        WriteLog(LogWriter.EventType.eError, ex.StackTrace.ToString)
  End Try

 Protected Sub bt_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs
    Dim a As String
    a = lbllogname.Text
    Response.ContentType = "text/plain"
    Response.AppendHeader("Content-Disposition", "attachment; filename=" & a)
    Response.TransmitFile(Server.MapPath("~/logs/" & a))
    Response.End()

End Sub
4

1 回答 1

0

要使用您在代码中构建的动态表结构,您需要对每一行中的每个按钮进行唯一命名;否则按钮单击处理程序 ( bt_Click) 无法确定打开文件的正确行,因为它们都被称为相同的并且将使用最后一个。

既然你想要一个表结构,那么我建议你使用GridView服务器控件,因为它会提供类似的输出,但提供使用模板将每一行的控件命名为相同的能力,但允许你在一个点击事件发生。

于 2013-11-09T12:51:14.997 回答