我使用Oftype
生成动态文本框,但我的视觉工作室不支持Oftype
。我需要不同的控制或任何其他方法来完成这项任务。当我运行此代码时:
Protected Sub AddTextBox(sender As Object, e As EventArgs)
Dim index As Integer = pnlTextBoxes.Controls.OfType(Of TextBox)().ToList().Count + 1
Me.CreateTextBox("txtDynamic" & index)
End Sub
我收到此错误:
'OfType'不是'System.Web.UI.ControlCollection'
的成员
当我使用面板生成动态文本框时。
Protected Sub Page_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
Dim content As ContentPlaceHolder = DirectCast(Me.Master.FindControl("MainContent"), ContentPlaceHolder)
Dim keys As List(Of String) = Request.Form.AllKeys.Where(Function(key) key.Contains("txtDynamic")).ToList()
Dim i As Integer = 1
For Each key As String In keys
Me.CreateTextBox("txtDynamic" & i)
i += 1
Next
End Sub
Private Sub CreateTextBox(id As String)
Try
Dim txtDyn_sub_h As New TextBox()
txtDyn_sub_h.ID = id
txtDyn_sub_h.CssClass = "table_row"
txtSubHd = txtDyn_sub_h.Text
pnlTextBoxes.Controls.Add(txtDyn_sub_h)
Dim lt As New Literal()
lt.Text = "<br />"
pnlTextBoxes.Controls.Add(lt)
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
答案 0 :(得分:0)
您应该检查以确保包含对System.Linq
的引用,并且在项目属性的Imported namespaces
部分中选择了它。
System.Linq
旁边有一个勾号。答案 1 :(得分:0)
如果您真的无法像其他人建议的那样修复Linq参考,您可以手动迭代它们:
Dim Count As Integer
For Each C As Control In pnlTextBoxes.Controls
If TypeOf C Is TextBox Then Count += 1
Next
Me.CreateTextBox("txtDynamic" & Count)
但是你真的需要为你的文本框指定名字吗?您应该存储已创建实例的引用以进行更好的处理。