1

我有一个不是动态创建的按钮。当我单击该按钮时,会计算文本框中的行数。我将其存储在名为 count 的变量中。根据计数值,我在面板中创建按钮。

到目前为止,它工作正常。

现在这些按钮的点击事件没有触发。

我试图用谷歌搜索我的问题。但在任何地方我都得到了相同的答案。Page_Init在事件中创建控件。但怎么可能呢?我从文本文件的行中获取 count 的值,而 count 的值决定了面板中将创建多少个按钮。

Dim btnAllow As New Button
btnAllow.Text = "Allow"
btnAllow.ID = "btA" & x
AddHandler btnAllow.Click, AddressOf btnAllow_Click

btnAllowList.Add(btnAllow)

tdAllow.Controls.Add(btnAllow)

btnAllow_Click 的代码

Protected Sub btnAllow_Click(ByVal sender As Object, ByVal e As System.EventArgs)
   For x As Integer = 0 To btnAllowList.Count - 1
      If sender.ID.SubString(3) = btnAllowList(x).ID.Substring(3) Then
         Dim lineCount = IO.File.ReadAllLines(PendingRequestsPath).Length
         Dim reader As New System.IO.StreamReader(DocumentViewersPath)
         Dim allLines As New List(Of String)
         Do While Not reader.EndOfStream
            allLines.Add(reader.ReadLine())
         Loop
         reader.Close()
         Dim DocumentViewerAlreadyExists As Boolean = False
         For i As Integer = 0 To lineCount - 1
            If ReadLine(i, allLines) = lblRequestorsList(x).Text Then
               DocumentViewerAlreadyExists = True
               Exit For
            End If
         Next
         If DocumentViewerAlreadyExists = False Then
            Dim Writer As System.IO.StreamWriter = IO.File.AppendText(DocumentViewersPath)
            Writer.WriteLine(lblRequestorsList(x).Text)
         End If
         Dim line As String = ""
         Dim r As IO.StreamReader = IO.File.OpenText(PendingRequestsPath)
         Dim strFile As New ArrayList
         While r.Peek <> -1  ' Loop through the file
            line = r.ReadLine 'Read the next available line
            ' Check to see if the line contains what you're looking for.
            ' If not, add it to an ArrayList
            If Not line.Contains(lblRequestorsList(x).Text) Then
               strFile.Add(line)
            End If
            r.Close()
            ' Because we want to replace the content of the file, we first
            ' need to delete it.
            If IO.File.Exists(PendingRequestsPath) Then
               IO.File.Delete(PendingRequestsPath)
            End If
            ' Create a StreamWriter object with the same filename
            Dim objWriter As New IO.StreamWriter(PendingRequestsPath, True)
            ' Iterate through the ArrayList
            For Each item As ArrayList In strFile
               objWriter.WriteLine(item) ' Write the current item in the ArrayList to the file.
            Next
            objWriter.Flush()
            objWriter.Close()
         End While
      End If
   Next
End Sub
4

1 回答 1

1

这对我有用:

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    Dim NumberOfControls As Integer = 10
    Session("CreateControls") = NumberOfControls
End Sub

Protected Sub btnAllow_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    'This will be executed when clicking on the newly created buttons.
End Sub

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    If Session("CreateControls") IsNot Nothing Then
        For x As Integer = 0 To Convert.ToInt32(Session("CreateControls"))
            Dim btnAllow As New Button
            btnAllow.Text = "Allow"
            btnAllow.ID = "btA" & x
            AddHandler btnAllow.Click, AddressOf btnAllow_Click

            Panel1.Controls.Add(btnAllow)
        Next
    End If
End Sub
于 2013-04-26T21:50:59.747 回答