locked
Set ALL fields to be ReadOnly that is on a web page RRS feed

  • Question

  • User-1547820999 posted

    Hello,

    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.

    Thanks!

    Eddi Rae

    Monday, October 14, 2013 10:28 AM

Answers

  • User281315223 posted

    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
                End If
    Next

    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




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 14, 2013 12:21 PM

All replies

  • User-344705538 posted

    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

    End If

    Next

    End If

    Next

    End Sub

    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."


    Monday, October 14, 2013 11:41 AM
  • User281315223 posted

    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
                End If
    Next

    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




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 14, 2013 12:21 PM
  • User-1547820999 posted

    Thanks so much for the input.  This is what I am looking for.

     

    Have a great week!!!

    Eddi Rae

    Monday, October 14, 2013 12:31 PM