[英]ASP.net webforms - accessing controls
我的问题是:
我有一个看起来像这样的网页:
<asp:PlaceHolder ID="Formular" runat="server">
<table>
<tr runat="server" visible="true" id="1">
<td> <asp:TextBox ID="TextBox13" runat="server" AutoPostBack="true" OnTextChanged="tb_Changed" CssClass="tx"></asp:TextBox>
</td>
</table>
</asp:PlaceHolder>
现在,我想循环浏览网页上的所有控件,但是,当然,我无法使用以下代码访问该TextBox13:
Dim tb as TextBox
For Each ctrl In Formular.Controls
If TypeOf ctrl Is TextBox Then
tb = ctrl
If tb.Text.Trim.Length = 0 Then
tb.Style("background-color") = "red"
count += count + 1
Else
tb.Style("background-color") = "white"
End If
End If
Next
有什么优雅的简便方法可以访问该文本框?
我之所以不使用javascript隐藏该表行,是因为此页面的代码将在以后的其他地方使用,并且将比不使用任何javascript更加容易。
您可以通过以下方式访问文本框的对象:
Dim szTextbox As String = Left(Request.Form("TextBox13"), 50)
以字符串形式获取内容
TextBox13.Text
在文本框控件中获取和设置字符串
更新
基于查找特定的文本框。
递归浏览页面并根据id查找文本框:
Public Function FindControlRecursive(Of ItemType)(ByVal Ctrl As Object, ByVal id As String) As ItemType
If String.Compare(Ctrl.ID, id, StringComparison.OrdinalIgnoreCase) = 0 AndAlso TypeOf Ctrl Is ItemType Then
Return CType(Ctrl, ItemType)
End If
For Each c As Control In Ctrl.Controls
Dim t As ItemType = FindControlRecursive(Of ItemType)(c, id)
If t IsNot Nothing Then
Return t
End If
Next
Return Nothing
End Function
来自的代码: 遍历文本框和标签
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.