【问题标题】:Handling events of dynamically created controls in asp.net在 asp.net 中处理动态创建的控件的事件
【发布时间】:2013-04-21 03:27:52
【问题描述】:

我有一个不是动态创建的按钮。当我单击该按钮时,会计算文本框中的行数。我将其存储在名为 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

【问题讨论】:

  • 分享你用于创建这些按钮的代码我的朋友。
  • 代码按照你说的编辑
  • 完美。现在请包括你有这个代码的事件,以及 btnAllow_Click 方法:)
  • 上面创建btnAllow的代码在btnRequest的点击事件上。
  • 什么是 btnAllowList 和 tdAllow 控件?我正在尝试构建场景:p

标签: asp.net vb.net button click


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 2012-03-18
    • 1970-01-01
    • 2017-07-22
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多