请看下面的代码:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim c1 As New CheckBox
c1.ID = "Test1/" 'line 7
PlaceHolder1.Controls.Add(c1)
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each c As Control In Page.Controls
For Each d As Control In c.Controls
For Each f As Control In d.Controls
For Each g As Control In f.Controls
For Each h As Control In g.Controls
If TypeOf h Is CheckBox Then
Dim cbox As New CheckBox
cbox = CType(h, CheckBox)
MsgBox(cbox.Checked) 'Line 20
End If
Next
Next
Next
Next
Next
End Sub
End Class
default.aspx 页面上有一个占位符,名为:PlaceHolder1。第 20 行的 MessageBox 正在打印 True。请查看以下网页中的引用:http: //msdn.microsoft.com/en-us/library/system.web.ui.control.id.aspx
“只有字母数字字符和下划线字符 (_) 的组合是此属性的有效值。包含空格或其他无效字符将导致 ASP.NET 页面解析器错误。”
/ 不是字母数字字符,那么消息框怎么打印为真呢?如果将第 7 行更改为 : c1.ID = "Test1:"
,那么这会像我预期的那样打印为 false (因为 : (冒号)不是字母数字)。没有一致性。我错过了一些东西。