表格有三列,分别放着动态加载的下拉列表, 单选按钮列表和FileUpload控件。
表格的行数以表格上方的文本框输入的数值为准,从1开始循环,动态生成tableRow,然后每个tableRow动态生成三个tableCell,tableCell中放着动态生成的WEB服务器控件,就是上面那几个。
具体代码参考了Insus的输入数字动态创建行
- VB.NET code
Private Sub GenerateTable(rows As Integer) Dim tableRows As Integer = rows '接收用户设置行数 Dim tableCells As Integer = 4 '4列 Dim tbId As Integer = 1 '由于一行有多个Textbox,要设置每个TextBox的ID唯一性。 For i As Integer = 1 To tableRows Dim tableRow As New TableRow() For j As Integer = 1 To tableCells Select Case j Case 1 '第一列 Dim tableCell1 As New TableCell() tableCell1.ClientIDMode = UI.ClientIDMode.Static tableCell1.ID = "tableCell1_" & i.tostring tableCell1.BorderWidth = Unit.Pixel(1) '添加资料类型下拉列表 Dim lstDocType As New DropDownList() lstDocType.ClientIDMode = UI.ClientIDMode.Static lstDocType.ID = "lstDocType" & i.ToString tableCell1.Controls.Add(lstDocType) tableRow.Cells.Add(tableCell1) Exit Select Case 2 '第二列 Dim tableCell2 As New TableCell() tableCell2.ClientIDMode = UI.ClientIDMode.Static tableCell2.ID = "tableCell2_" & i.tostring tableCell2.BorderWidth = Unit.Pixel(1) '添加语言选项 Dim chkDocLan As New RadioButtonList() chkDocLan.ClientIDMode = UI.ClientIDMode.Static chkDocLan.ID = "chkDocLan" & i.ToString chkDocLan.Items.Add("中文") chkDocLan.Items.Add("英文") chkDocLan.Items(0).Selected = True tableCell2.Controls.Add(chkDocLan) tableRow.Cells.Add(tableCell2) Exit Select Case 3 '第三列 Dim tableCell3 As New TableCell() tableCell3.ClientIDMode = UI.ClientIDMode.Static tableCell3.ID = "tableCell3_" & i.tostring tableCell3.BorderWidth = Unit.Pixel(1) '添加上传控件 Dim uploadDoc As New FileUpload() uploadDoc.ClientIDMode = ClientIDMode.[Static] uploadDoc.ID = "uploadDoc" & i.ToString() tableCell3.Controls.Add(uploadDoc) tableRow.Cells.Add(tableCell3) Exit Select End Select Next tableRow.ClientIDMode = UI.ClientIDMode.Static tableRow.ID = "rowUploadDoc" & i.ToString TableUploadLayout.Rows.Add(tableRow) Next End Sub
查找控件的代码
- VB.NET code
Protected Sub btnUploadDoc_Click(sender As Object, e As System.EventArgs) Handles btnUploadDoc.Click Dim intDocNumber As Integer = Convert.ToInt32(lblDocNumber.Text) '上传完毕后提示结果! Dim strReport As String strReport = "共上传了" & intDocNumber.ToString & "份资料:" & vbCrLf For i As Integer = 1 To intDocNumber Dim rowUploadDoc As TableRow = TableUploadLayout.FindControl("rowUploadDoc" & i.ToString) '上面这个还能找到,往下就找不到了 Dim tableCell1 As TableCell = rowUploadDoc.FindControl("tableCell1_" & i.ToString) Dim tableCell2 As TableCell = rowUploadDoc.FindControl("tableCell2_" & i.ToString) Dim tableCell3 As TableCell = rowUploadDoc.FindControl("tableCell3_" & i.ToString) Dim lstDocType As DropDownList = tableCell1.FindControl("lstDocType" & i.ToString) Dim chkDocLan As RadioButtonList = tableCell2.FindControl("chkDocLan" & i.ToString) Dim uploadDoc As FileUpload = tableCell3.FindControl("uploadDoc" & i.ToString) ... '后续操作 Next ... '后续操作End Sub