简体   繁体   中英

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. 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'

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.

  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.

If you really can not fix the Linq reference as others suggested, you can iterate them manually:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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