Last post Oct 14, 2013 12:31 PM by EddiRae
Oct 14, 2013 10:28 AM|EddiRae|LINK
I am looking to change the ReadOnly to all the fields on my web page. The text fields are named "resp_txt_xxxxx" where xxxxx is a number to make them unique.
I want to not have to code ALL of these fields (there are over 200 fields).
If anyone has done this, I would be greatly appreciative if you share.
Oct 14, 2013 11:41 AM|anilreddy128|LINK
You can try something like below...
Private Sub GetTextboxes()
Dim textBoxString As String
textBoxString = "System.Web.UI.WebControls.TextBox"
For Each control1 In Page.Controls
If control1.Controls.Count > 0 Then
For Each control2 In control1.Controls
If control2.GetType.ToString = textBoxString Then
Dim currentTextBox As New TextBox()
currentTextBox = CType(control2, TextBox)
currentTextBox.Text = "New Text"
currentTextBox.BackColor = System.Drawing.Color.Yellow
This sub loops through the Web page's control collection. This collection consists of all controls on the page. An inner loop checks each of those controls for any child controls. When the code finds a "TextBox" control, it stores that
control in the variable named "currentTextBox."
Oct 14, 2013 12:21 PM|Rion Williams|LINK
You should be able to either loop through all of the available Controls within your Page / Form and set the ReadOnly property to "False" as seen below :
'Loop through each Control within your Page'
For Each control As Control In Controls
'Check if it is a TextBox'
If TypeOf control Is TextBox Then
'If so - cast it properly'
Dim yourTextBox As TextBox = DirectCast(control, TextBox)
'Set it to ReadOnly'
yourTextBox.ReadOnly = True
If you wanted to have the values not be editable at all, you might consider using the Disabled property instead :
yourTextBox.Disabled = True
which could also simply be applied to the entire form itself :
Form.Disabled = True
Oct 14, 2013 12:31 PM|EddiRae|LINK
Thanks so much for the input. This is what I am looking for.
Have a great week!!!