处理asp.net中动态创建的控件的事件 [英] Handling events of dynamically created controls in asp.net
问题描述
我有没有动态创建一个按钮。当我点击该按钮,在一个文本框的行数进行计数。我存储在一个名为计数的变量。根据计值我在面板上创建按钮。
到现在为止它正常工作。
现在这些按钮的点击事件不会开火。
我曾尝试谷歌我的问题。但无论我得到了同样的答案。创建 Page_Init
事件的控件。但是,怎么可能?我从一个文本文件的线条让计的价值,并计数该值决定多少个按钮在面板中创建。
昏暗btnAllow作为新按钮
btnAllow.Text =允许
btnAllow.ID =BTA与& X
AddHandler的btnAllow.Click,AddressOf btnAllow_ClickbtnAllowList.Add(btnAllow)tdAllow.Controls.Add(btnAllow)
在code为btnAllow_Click
保护小组btnAllow_Click(BYVAL发件人为对象,BYVAL E上System.EventArgs)
当x为整数= 0〜btnAllowList.Count - 1
如果sender.ID.SubString(3)= btnAllowList(x)的.ID.Substring(3)然后
昏暗lineCount = IO.File.ReadAllLines(PendingRequestsPath)。长度
昏暗的读者作为新就是System.IO.StreamReader(DocumentViewersPath)
昏暗allLines作为新的列表(串)
这样做虽然不reader.EndOfStream
allLines.Add(reader.ReadLine())
循环
reader.Close()
昏暗DocumentViewerAlreadyExists由于布尔= FALSE
对于我作为整数= 0〜lineCount - 1
如果的ReadLine(I,allLines)= lblRequestorsList(X)。文本然后
DocumentViewerAlreadyExists = TRUE
对于出口
万一
下一个
如果DocumentViewerAlreadyExists = false,那么
昏暗的作家作为System.IO.StreamWriter = IO.File.AppendText(DocumentViewersPath)
Writer.WriteLine(lblRequestorsList(X)。文本)
万一
昏暗的行作为字符串=
昏暗 - [R作为IO.StreamReader = IO.File.OpenText(PendingRequestsPath)
昏暗strFile作为新的ArrayList
虽然r.Peek<> -1遍历文件
行= r.ReadLine'读取下一个可用行
'检查,看看是否行包含您要查找的内容。
如果没有,将它添加到一个ArrayList
如果不line.Contains(lblRequestorsList(X)。文本)然后
strFile.Add(线)
万一
r.Close()
因为我们要,我们首先替换文件的内容
需要将其删除。
如果IO.File.Exists(PendingRequestsPath)然后
IO.File.Delete(PendingRequestsPath)
万一
'创建具有相同文件名的StreamWriter对象
昏暗objWriter作为新IO.StreamWriter(PendingRequestsPath,真)
通过ArrayList的迭代
对于每个项目的ArrayList在strFile
objWriter.WriteLine(项目)ArrayList中的文件写入当前项目。
下一个
objWriter.Flush()
objWriter.Close()
虽然结束
万一
下一个
结束小组
这为我工作:
保护小组的button1_Click(发送者为对象,E作为System.EventArgs)把手Button1.Click
昏暗NumberOfControls为整数= 10
会议(CreateControls)= NumberOfControls
结束小组保护小组btnAllow_Click(BYVAL发件人为对象,BYVAL E上System.EventArgs)
这将在新创建的按钮点击时被执行。
结束小组保护小组的Page_Load(发送者为对象,E作为System.EventArgs)把手Me.Load
如果会议(CreateControls)状态并没有任何再
当x为整数= 0〜Convert.ToInt32(会话(CreateControls))
昏暗btnAllow作为新按钮
btnAllow.Text =允许
btnAllow.ID =BTA与& X
AddHandler的btnAllow.Click,AddressOf btnAllow_Click Panel1.Controls.Add(btnAllow)
下一个
万一
结束小组
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
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
这篇关于处理asp.net中动态创建的控件的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!