我正在尝试在包含文本框和下拉列表的页面上循环控制并清除它们。
当我调试时,parent是当前页面,value等于ASP.nameOfCurrentpage_aspx和 Type等于system.web.ui.page,但c的值为ASP.site_master,类型为system.web.ui.control。我还放入x来查看它找到了多少控件,即使页面上有15个左右的文本框,x也会返回1。有没有办法可以强制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
答案 0 :(得分:1)
HTMLForm控件可能是嵌套的,可能是在MasterPage下。要么完全递归函数,要么在If语句中添加一个查找母版页的子句。这是断点和观察窗是黄金的地方。
例如:
ElseIf c.GetType.ToString = "ASP.MasterPageName_Master" Then
ClearForm(c)
答案 1 :(得分:1)
谢谢大家的帮助。我没有尝试所有的答案,但我用它们的想法。这就是我们在工作中提出的,它可以找到并清除页面上的所有控件。我们必须找到链接到主站点(cph)的内容占位符。再次感谢所有的建议。
Public Sub ClearForm(ByRef Parent As Control)
Dim cph As System.Web.UI.WebControls.ContentPlaceHolder = Master.FindControl("MainBody")
Dim c As Control
Dim x As Integer = Parent.Controls.Count
For Each c In cph.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
答案 2 :(得分:0)
自从我使用控件编写.NET中的任何内容以来,已经有一段时间了,MVC愚蠢地破坏了我,或者使用VB.NET ......
但是我想你可能需要通过一堆控件来递归:
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()
ElseIf c.Controls != null ' Does VB.NET support nulls? I forget
ClearForm(c.Controls)
End If
Next
End Sub