Vishal
Vishal

Reputation: 6378

Handling events of dynamically created controls in asp.net

I have a button which is not created dynamically. When I click on that button the number of lines in a textbox are counted. I store that in the variable called count. Depending on value of count I create buttons in panel.

Up to now it is working properly.

Now the click event of those buttons is not firing.

I have tried to google my question. But everywhere I got the same answer. Create the controls in Page_Init event. But how is it possible? I am getting the value of count from a textfile's lines, and that value of count decides how many button will be created in the panel.

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)

The code for 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

Upvotes: 1

Views: 4351

Answers (1)

Hanlet Esca&#241;o
Hanlet Esca&#241;o

Reputation: 17380

This worked for me:

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

Upvotes: 1

Related Questions