Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

need to get webcontrol names

Hi my webform currently uses about 70 webcontrols . I need to obtain a list of all the controls ( their ID property ) . Anyone know how I could do this ? Thanks :D
[167 byte] By [Matrix.net] at [2007-11-11 10:15:50]
# 1 Re: need to get webcontrol names
Which version of ASP.NET are you using?
Phil Weber at 2007-11-11 23:11:58 >
# 2 Re: need to get webcontrol names
ASP.net 2.0 in production server
ASP.net 1.1 in my pc
Matrix.net at 2007-11-11 23:13:04 >
# 3 Re: need to get webcontrol names
In ASP.NET 1.x, the controls are declared in your code-behind file. You may get the control IDs by simply copying the block of code that looks like this:

Protected lblName As System.Web.UI.WebControls.Label
Protected txtName As System.Web.UI.WebControls.TextBox
Protected lblThankYou As System.Web.UI.WebControls.Label
Protected WithEvents btnEnter As System.Web.UI.WebControls.Button

The IDs in the above example are lblName, txtName, lblThankYou and btnEnter.

If you want to use code to get the control IDs, you may do this:

For Each ctl As Control In FormName.Controls
If Len(ctl.ID) > 0 Then
Response.Write(ctl.ID & "<br />")
End If
Next
Phil Weber at 2007-11-11 23:14:02 >
# 4 Re: need to get webcontrol names
In ASP.NET 1.x, the controls are declared in your code-behind file. You may get the control IDs by simply copying the block of code that looks like this:

Protected lblName As System.Web.UI.WebControls.Label
Protected txtName As System.Web.UI.WebControls.TextBox
Protected lblThankYou As System.Web.UI.WebControls.Label
Protected WithEvents btnEnter As System.Web.UI.WebControls.Button

The IDs in the above example are lblName, txtName, lblThankYou and btnEnter.

If you want to use code to get the control IDs, you may do this:

For Each ctl As Control In FormName.Controls
If Len(ctl.ID) > 0 Then
Response.Write(ctl.ID & "<br />")
End If
Next

THanks Phil, this should come in helpful , only problem is that I dont want the labels , just the dropdowns and textboxes.
Matrix.net at 2007-11-11 23:15:03 >
# 5 Re: need to get webcontrol names
You said, "I need to obtain a list of all the controls." ;-)

To list only TextBoxes and DropDownLists, you can do this:

For Each ctl As Control In frmMain.Controls
If TypeOf ctl Is TextBox OrElse _
TypeOf ctl Is DropDownList Then
Response.Write(ctl.ID & "<br />")
End If
Next
Phil Weber at 2007-11-11 23:16:07 >