2013-04-26 156 views
1

我有一個不是動態創建的按鈕。當我點擊那個按鈕時,文本框中的行數就會被計數。我將它存儲在名爲count的變量中。根據計數值我在面板中創建按鈕。在asp.net中處理動態創建的控件的事件

到目前爲止它工作正常。

現在,這些按鈕的點擊事件不會觸發。

我試圖谷歌我的問題。但到處都有同樣的答案。在Page_Init事件中創建控件。但是怎麼可能呢?我從textfile的行中獲取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 
+0

分享你用來創建這些按鈕我的朋友的代碼。 – 2013-04-26 21:06:47

+1

按照您的說明編輯代碼 – Vishal 2013-04-26 21:09:56

+0

完美。現在請包括您有此代碼的事件以及btnAllow_Click方法:) – 2013-04-26 21:10:22

回答

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 
相關問題