Monday, March 26, 2012

web control tree

i have a asp.net webpage, how could i list all the controls with the id begins with "textbox"?
i want to clear all the textboxes in the page without specifying the list.
is there a "control navigator" or a better way for me to do this?
thankxYou can iterate through the Page.Controls() collection

Dim ct As Control

For Each ct In Page.Controls
If ct.ID.StartsWith("textbox") Then
CType(ct, TextBox).Text = String.Empty
End If
Next

Something like that, I haven't tested that, so don't expect it to work :)
--Michael
You will have to go through each control's own Controls property very much like Michael did for the Page loop. And be sure to use a case insensitive match.

Function FindByName(ByVal pParentControl As Control, ByVal pNameToMatch As String)
For Each ct In pParentControl.Controls
If ct.ID.ToUpper().StartsWith(pNameToMatch.ToUpper()) Then
' found it. do something here
End If
FindByName(ct, pNameToMatch) ' Handle child controls
Next
End Function

Call it like this:
FindByName(Page, "TextBox")
i got the solution, thankx.
the crucial thing i need to do is to call that FindByName recursively.
to make the utility working more effectively, i need to check the type of the control, instead of the id/name...
if your control are in panel then
you have to loop inside the respective panel.

HTH

Hemant

0 comments:

Post a Comment