简体   繁体   English

在vb.net中我应该使用什么代替Oftype?

[英]what should i use instead of Oftype in vb.net?

I am generating dynamic textbox using Oftype , but Oftype is not supported by my visual studio. 我正在使用Oftype生成动态文本框,但是Oftype不支持Oftype I need different control or any other method to do this task. 我需要其他控件或任何其他方法来执行此任务。 When I run this code: 当我运行此代码时:

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

I get this error: 我收到此错误:

'OfType' is not a member of 'System.Web.UI.ControlCollection' 'OfType'不是'System.Web.UI.ControlCollection'的成员

When i am generating dynamic textbox using panel. 当我使用面板生成动态文本框时。

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

You should check to make sure you have included a reference to System.Linq and that it is selected in the Imported namespaces section of the Project Properties. 您应该检查以确保已包含对System.Linq的引用,并且已在“项目属性”的“ Imported namespaces部分中将其选中。

  1. In the Solution Explorer, right click the Project and select Properties 在解决方案资源管理器中,右键单击“项目”,然后选择“属性”
  2. Select the References section 选择参考部分
  3. In the bottom list, for Imported namespaces, make sure System.Linq has a tick next to it. 在底部列表中,对于“导入的名称空间”,请确保System.Linq旁边有一个勾号。

If you really can not fix the Linq reference as others suggested, you can iterate them manually: 如果您确实不能如其他建议一样修复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)

but do you really need to assign names to your textboxes? 但是您真的需要为文本框分配名称吗? You should store the reference of created instance for furher processing. 您应该存储所创建实例的引用以进行进一步处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM