积极答复者
为什么我按了动态生成的控件 myLBut 后,不能调用myLBut_click

问题
-
Protected Sub butFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butFind.Click
Dim FindFileName As New FindFile
Dim strFindPath As String = "F:\Visual Studio 2008\WebSites\FileSys\Files\SurveillanceCenterFiles"
Dim strFindName As String = TxtFindFileName.Text
Dim myFileSystemInfo As FileSystemInfo
myFileSystemInfo = FindFileName.FindFile(strFindName, strFindPath)
If myFileSystemInfo IsNot Nothing Then
AddTableHead()
Dim tRow As New TableRow
Dim tCells As TableCell()
tCells = New TableCell(4) {New TableCell, New TableCell, New TableCell, New TableCell, New TableCell}
tCells(0).Text = myFileSystemInfo.Name
If myFileSystemInfo.Attributes = FileAttributes.Directory Then
tCells(1).Text = "目录"
Else
tCells(1).Text = "文件"
End If
Dim myLBut As New LinkButton
myLBut.Text = "操作类型"
myLBut.ID = "myLBut"
AddHandler myLBut.Click, AddressOf myLBut_clicktCells(2).Controls.Add(myLBut)
tCells(3).Text = "上传时间"
tCells(4).Text = "上传人"
tRow.Cells.AddRange(tCells)
TableFilesBrowe.Rows.Add(tRow)
End If
End Sub
Sub myLBut_click()
AddTableHead()
'Dim tRow As New TableRow
'Dim tCells As TableCell()
'tCells = New TableCell(4) {New TableCell, New TableCell, New TableCell, New TableCell, New TableCell}
'tCells(0).Text = "fjdk"
'tCells(1).Text = "fjdk"
'tCells(2).Text = "fjdk"
'tCells(3).Text = "fjdk"
'tCells(4).Text = "fjdk"
'tRow.Cells.AddRange(tCells)
'TableFilesBrowe.Rows.Add(tRow)
End Sub
答案
-
你好!
我在下面地址回复了你的问题,请看:http://social.microsoft.com/Forums/zh-CN/295/thread/061dfde6-74d8-4f1c-bbbd-5aa3c9637d64
知识改变命运,奋斗成就人生!- 已标记为答案 KeFang Chen 2010年4月14日 3:18
全部回复
-
在你的界面上添加一个 PlaceHolder,ID 为 “PlaceHolder1”
示例代码:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) ' 动态创建的控件,不能放在 if (!IsPostBack) 判断中。 ' 下面注释的代码,事件不能引发。 'if (!IsPostBack) ' this.CreateControls(); Me.CreateControls() End Sub Private Sub CreateControls() For i As Integer = 0 To 4 Dim button As New Button() button.ID = String.Format("button{0}", i) button.Text = String.Format("button{0}", i) AddHandler button.Click, AddressOf button_Click Me.PlaceHolder1.Controls.Add(button) Next End Sub Private Sub button_Click(ByVal sender As Object, ByVal e As EventArgs) Response.Write(DirectCast(sender, Button).Text) End Sub
知识改变命运,奋斗成就人生! -
看多了头晕,让人不知所措。
换个方法问下:
我要实现这样一个操作,窗体上有一个table控件,和button控件,,通过编程,当我按下button控件时,服务器会在table中创建了一个row,row中有两个cell,其中一个cell是linkbutton控件,一个cell显示字符,linkbutton控件有个事件,可以改写row的第一个cell的值。
但目前的问题是:按下窗体上的button没问题,可以正常显示出row中的cell和linkbutoon控件,但是我一按下linkbutton控件执行事件后,整个表都不见了。
正确的编程方法应该是怎样啊,急啊。。。。研究了6个小时都没研究出来,头痛。
当我按下linkbutton后,后台到底发生了一些什么事件导致表都不见了????
当我把 button_click中的代码放到page_load中就没有问题,为什么啊。。
我的table控件状态为什么没保存下来。
Partial Class _Default Inherits System.Web.UI.Page Dim cell1 As New TableCell Dim cell2 As New TableCell Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim row As New TableRow cell1.Text = "事件还未引发" row.Cells.Add(cell1) Dim mycon As New LinkButton mycon.Text = "测试" cell2.Controls.Add(mycon) row.Cells.Add(cell2) AddHandler mycon.Click,AddressOf clickevent Table1.Rows.Add(row) End Sub Protected Sub clickevent() Table1.Rows(0).Cells(1).Text = "事件已经引发" End Sub End Class
-
你好!
我在下面地址回复了你的问题,请看:http://social.microsoft.com/Forums/zh-CN/295/thread/061dfde6-74d8-4f1c-bbbd-5aa3c9637d64
知识改变命运,奋斗成就人生!- 已标记为答案 KeFang Chen 2010年4月14日 3:18