Is there a way to change multiple readonly textbox attributes programmatically

Is there a way to change multiple readonly textbox attributes in a .net program.

0


source to share


5 answers


Assuming your textboxes start with the same prefix and exist in the page controls collection:

string commonTextBoxPrefix = "txt";
foreach (Control c in this.Controls)
{
    if (c.GetType() == typeof(TextBox) &&
        c.Name.StartsWith(commonTextBoxPrefix))
    {
        ((TextBox)c).ReadOnly = True;
    }
}

      



This won't return the entire control hierarchy, though :)

+2


source


You can load them as an array and modify them with a loop



0


source


Yes,

txt.Attributes["ReadOnly"] = "true";

      

Just use a loop for that :)

or you don't have this attribute in your control tags.

you can use this code

txt.Attributes.Add("ReadOnly","true");

      

0


source


You can load the names of the textboxes into the list and change them that way, or you can load the textbox objects into the list and change them.

foreach(TextBox txt in List<TextBox>)
{
    txt.ReadOnly = true;
}

      

0


source


Yes there is.

Add each textbox to an array and then loop through the array changing the attributes.

For example:

Dim textBoxes() As TextBox = {TextBox1, TextBox2, TextBox3}
For Each item As TextBox In textBoxes
    item.ReadOnly = True
Next

      

0


source







All Articles