tags:

views:

469

answers:

5

Is there a way to change several textbox's readonly attribute programatically in .net.

A: 

You could load them as an array and change them with a loop

A: 

Yes,

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

Just use a loop for that :)

or you haven't that attribute in your controls tags.

you can either use that code

txt.Attributes.Add("ReadOnly","true");
Braveyard
A: 

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

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

fARcRY
A: 

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
Matt Hanson
+2  A: 

Assuming your textboxes all begin 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 will not recurse the entire control hierarchy though :)

Neil