键入检查WebControl

时间:2012-12-11 16:20:31

标签: asp.net vb.net

我在VBA中知道如何对控件Type进行Select..Case比较,如下:

Select Case TypeName(ctrl)
  case is = "ListBox"
    ...
  case is = "ComboBox"
    ...
  ...
End Select

在VB.Net中,我可以使用上面的常规值,还是我必须在文本中使用命名空间限定符?

目前正在实施:

public function Validate(byref ctrl as WebControl) as boolean
  select case TypeName(ctrl)
    case is = "TextBox"
      ....
    case is = "Label"
      ....
    ...
  End select
End Function

2 个答案:

答案 0 :(得分:4)

您不需要该类型的“名称”,您可以直接使用该类型:

    Select Case True
        Case TypeOf c Is TextBox
            ' its a Textbox
        Case TypeOf c Is Label
            ' its a label
        Case Else
            'foo
    End Select

答案 1 :(得分:2)

我认为你期待这样的事情 您可以使用TypeOf Operator

Dim ctrl As Control
        For Each ctrl Me.Controls
            If (TypeOf ctrl Is TextBox) Then
                ''do something
            End If
            If (TypeOf ctrl Is Label) Then
                ''do something
            End If
        Next ctrl 

<强>更新

使用case

  select case True
    case  TypeOf ctrl Is TextBox
      ....
    case TypeOf ctrl Is Label
      ....
    ...
  End select