Asp.net| Traversal | control | page "How do I traverse all the controls on the asp.net page?" "This is one of the most common problems in the community asking questions." Usually our answer to this question is: "Use the controls property of the page class to implement"! This controls property allows us to get all the child controls of a control, but if one of the child controls also has its own child control, it is very difficult to get all the controls on the ASP.net page simply by using this property. So, to solve this problem fundamentally, we need to write some extra methods to get all the controls on the page.
Assuming that there are several textbox in the page, we want to traverse the entire page and get the name and value of all the textbox and display them in the DataGrid.
Before we start traversing the page, we need to create a class that holds the name and value of those textbox, and the code is as follows:
Public Class Utilityobj
Private _name as String
Private _value as String
Public Sub New (ByVal Name As String, ByVal Value as String)
_name = Name
_value = value
End Sub
Public Property Name () as String
Get
return _name
End Get
Set (ByVal Value as String)
_name = Name
End Set
End Property
Public Property Value () as String
Get
Return (_value)
End Get
Set (ByVal Value as String)
_value = value
End Set
End Property
End Class
This class contains two attributes: "Name" and "value", and then defines a public ArrayList (oarraylist) for storing data. As shown in figure:
To implement all the controls that traverse the ASP.net page, we also need to define a major approach. This method receives a parameter of the control type and, if this argument is a TextBox, stores its name and value.
The code is as follows:
Public Sub Loopingcontrols (ByVal Ocontrol as Control)
Dim Frmctrl as Control
Oarraylist = New ArrayList
For each Frmctrl in Ocontrol.controls
If TypeOf Frmctrl is TextBox Then
Oarraylist.add (New utilityobj (frmctrl.id, DirectCast (Frmctrl, TextBox). Text))
End If
If Frmctrl.hascontrols Then
Loopingcontrols (Frmctrl)
End If
Next
End Sub
We can use this method to implement all the controls that traverse the ASP.net page
Loopingcontrols (Page)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.