通过VB Web应用程序的文本框迭代 [英] Iterate through textboxes in VB Web App

查看:154
本文介绍了通过VB Web应用程序的文本框迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有少数回答这样的问题,但他们似乎对我很清楚,我用jQuery没有经验,所以我问在这里。

There's a few answers to this sort of question, but none of them seem very clear to me and I have no experience with JQuery, so I'm asking here.

我有Default.aspx中一束它的文本框(在Visual Web设计2010例preSS使用的基本模板)一个VB Web应用程序。我想用某种形式的解决方案,VB如果可能的话,并清除它们,当用户presses一个按钮,通过这些文本框进行迭代。我一直在使用这样的尝试:

I have a VB Web Application with a bunch of textboxes on it in Default.aspx (Using the basic template in Visual Web Designer 2010 Express). I'd like to iterate through those textboxes using some sort of VB solution if at all possible and clear them when the user presses a button. I've tried using something like this:

Dim cControl As Control
For Each cControl in Me.Controls
    If cControl Is TextBox Then
        cControl.Text = ""
    EndIf
Next

但是,这并不正常工作。如果任何人都可以在正确的方向指向我,那将是巨大的。谢谢!

But this doesn't work. If anyone could point me in the right direction, that would be great. Thanks!

推荐答案

有2个问题与code。首先,使用code:

There are 2 problems with your code. First, using the code:

For Each cControl in Me.Controls
  ...
Next

如果所有的文本框的主要页面上

只会工作,而不是在面板上,在一个组框等。

will only work if all of the textboxes are on the main page, and not on a panel, in a group box, etc.

二,code

If cControl is Textbox Then

将失败,因为cControl是不完全相同的对象为文本框。你想成为检查,看看是否cControl的类型是文​​本框。递归解决您的code将是:

will fail because cControl is not the exact same object as Textbox. You want to be checking to see if the Type of cControl is Textbox. A recursive solution to your code would be:

Public Sub ClearTextBoxes (ctrl as Control)
  If Ctrl.HasChildren Then
    For each childCtrl as Control in Ctrl.Controls
      ClearTextBoxes(childCtrl)
    Next
  Else
    If TypeOf Ctrl is TextBox Then
      DirectCast(Ctrl, TextBox).Text = String.Empty
    End If
  End If
End Sub

要运行的方法,你会再调用:

To run the method, you would then call:

ClearTextBoxes(Me)

这篇关于通过VB Web应用程序的文本框迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆