我想循環通過包含文本框和下拉列表的頁面上的控件並清除它們。vb.net循環通過控件不起作用
當我調試時,parent是當前頁面,值等於ASP.nameOfCurrentpage_aspx和 Type等於system.web.ui.page,但c的值爲ASP.site_master和system.web的類型.ui.control。我還將x放在x中,查看它找到了多少個控件,並且x返回爲1,即使頁面上有15個左右的文本框。有沒有一種方法可以強制C具有ASP.nameOfCurrentpage_aspx的價值?或者那不是我的問題?任何幫助表示讚賞。
Protected Sub btnClear_Click(sender as Object, e as System.eventargs) Handles btnClear.Click
ClearForm(Page)
End Sub
Public Sub ClearForm(ByRef Parent As Control)
Dim c As Control
Dim x As Integer = Parent.Controls.Count
For Each c In Parent.Controls
If c.GetType.ToString = "System.Web.UI.HtmlControls.HtmlForm" Then
ClearForm(c)
ElseIf c.GetType() Is GetType(TextBox) Then
'is it a Text Box?
Dim t As TextBox = c
t.Text = ""
ElseIf c.GetType() Is GetType(DropDownList) Then
'is it a dropdown list?
Dim d As DropDownList = c
d.ClearSelection()
End If
Next
End Sub
@Taylor,感謝編輯我的帖子 – Kevin 2012-08-16 15:37:45