我已经在asp.net页面上动态创建了一个表,方法是根据SQL数据库中数据集中的行数创建行。我使用for循环在page_load()函数中使用填充的下拉列表创建我的行和单元格。我的表是根据需要创建的,但是我需要获取每行中每个单元格的选定项目文本,以便在单击提交按钮时存储回数据库表。我最初在我的asp页面上有一个占位符,并在加载时将其替换为创建的表。出于某种原因,当我尝试使用
时对于每个r作为TableRow在LineupTable.Rows
msgbox(EmployeeDDL.SelectedItem.Text)
下一个
它为employeeddl.selecteditem.text提供了一个空值错误。
这是我生成表格的方式。
'动态创建表行和单元格,并使用适当的控件和/或信息填充它们
Dim numrows As Integer = dtEQP.Rows.Count
Dim numcells As Integer = 6
Dim j As Integer
For j = 0 To numrows - 1
r = New TableRow()
c1 = New TableCell() With {.Width = 300}
c1.Controls.Add(New LiteralControl(dtEQP.Rows(j).ItemArray(0)))
r.Cells.Add(c1)
c2 = New TableCell() With {.Width = 200}
Dim EmployeeDDL As New DropDownList()
EmployeeDDL.DataSource = dsEMP
EmployeeDDL.DataTextField = "FirstName"
EmployeeDDL.DataValueField = "ID"
EmployeeDDL.DataBind()
EmployeeDDL.Items.Insert(0, New ListItem("", "-1"))
c2.Controls.Add(EmployeeDDL)
r.Cells.Add(c2)
c3 = New TableCell() With {.Width = 180}
Dim CodeDDL As New DropDownList()
CodeDDL.Items.Add("0-Running")
CodeDDL.Items.Add("99-Idle")
CodeDDL.Items.Add("60-Down")
CodeDDL.Items.Add("1-Weather")
CodeDDL.SelectedValue = "99-Idle"
c3.Controls.Add(CodeDDL)
r.Cells.Add(c3)
c4 = New TableCell() With {.Width = 100}
Dim RideDDL1 As New DropDownList()
RideDDL1.DataSource = dsRide
RideDDL1.DataTextField = "Ridename"
RideDDL1.DataValueField = "RideID"
RideDDL1.DataBind()
RideDDL1.Items.Insert(0, New ListItem("", "-1"))
c4.Controls.Add(RideDDL1)
r.Cells.Add(c4)
c5 = New TableCell() With {.Width = 200}
Dim OTddl1 As New DropDownList()
OTddl1.DataSource = dsEMP2
OTddl1.DataTextField = "FirstName"
OTddl1.DataValueField = "ID"
OTddl1.DataBind()
OTddl1.Items.Insert(0, New ListItem("", "-1"))
c5.Controls.Add(OTddl1)
r.Cells.Add(c5)
c6 = New TableCell() With {.Width = 350}
Dim CommentsTxtBx As New TextBox()
c6.Controls.Add(CommentsTxtBx)
r.Cells.Add(c6)
LineupTable.Rows.Add(r)
Next j
关于如何在点击子程序中检索此值以获取实际选择内容的任何想法?
答案 0 :(得分:0)
首先添加DropDownList,给它一个ID(使用你的游标变量来避免重复的ID):
EmployeeDDL.ID = "EmployeeDDL" & j.ToString()
其次,由于DropDownList是动态添加的,因此您需要使用FindControl来获取它的实例。另外,确保SelectedItem不为null / nothing。
Dim intCursor As Integer = 0
For Each r As TableRow In LineupTable.Rows
Dim ddlTarget As DropDownList = Cast(r.Cells(1).FindControl("EmployeeDDL" & intCursor.ToString()), DropDownList)
If Not ddlTarget.SelectedItem Is Nothing Then
Dim strValue = ddlTarget.SelectedItem.Text
End If
intCursor += 1
Next r