[Translation] Traversed ASP.NET Page Control

xiaoxiao2021-03-06  115

[Translation] Traversed ASP.NET Page Control

Looping through controls in asp.net

"How to traverse all controls all of the ASP.NET page?" This is one of the most common problems in the community. Usually our answer to this question is: "Implement using the controls attribute of the Page class"! This Controls property allows us to get all the child controls for a control, but if one of the sub-controls also have their own sub-control, it is difficult to get all the controls all of the ASP.NET page only using this property. Therefore, to solve this problem at all, we need to write some additional methods to get all the controls in the page.

Suppose there are several TextBox in the page, we want to traverse the entire page, then get all the TextBox's Name and Value, and display them in the DataGrid.

Before we start traversing page, you need to create a class to store Name and Value of TextBox, 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 properties: "name" and "value", define a public ArrayList (OARRAYLIST) for storing data. Figure:

To implement all controls all over the ASP.NET page, we also need to define a major approach. This method receives a parameter of a Control type. If this parameter is TextBox, it stores its Name and Value.

code show as below:

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 the

OARRAYLIST.ADD (New UtilityObj (frmctrl.id, directcast (frmctrl, textbox) .text))

END IF

IF frmctrl.hascontrols kil

LoopingControls (frmctrl)

END IF

NEXT

End Sub

We can use this method to realize all controls all over the ASP.NET page

LoopingControls (Page)

DataGrid1.datasource = OARRAYLIST

DataGrid1.databind ()

转载请注明原文地址:https://www.9cbs.com/read-125103.html

New Post(0)