【问题标题】:loop through each textbox on page循环浏览页面上的每个文本框
【发布时间】:2013-03-04 10:05:12
【问题描述】:

我想遍历网络表单上的每个文本框并清除控件中的所有文本..

我试过了:

    Dim ctrl As TextBox

    For Each ctrl In form1.Controls
        ctrl.Text = ""
    Next

但我收到一条错误消息:

无法将“System.Web.UI.LiteralControl”类型的对象转换为“System.Web.UI.WebControls.TextBox”类型。

有什么想法吗?

【问题讨论】:

    标签: asp.net vb.net


    【解决方案1】:

    试试这个

    For Each control As Object In form1.Controls
            If TypeOf control Is TextBox Then
                control.Text = ""
            End If
    
        Next
    

    【讨论】:

      【解决方案2】:

      使用此代码

      For Each c As Control In Page.Controls
      If TypeOf c Is TextBox Then
          TryCast(c, TextBox).Text = [String].Empty
      End If
      Next
      

      【讨论】:

        猜你喜欢
        • 2014-09-23
        • 1970-01-01
        • 1970-01-01
        • 2020-12-13
        • 1970-01-01
        • 2010-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多