Reputation:
Is there a way to change several textbox's readonly attribute programatically in .net.
Upvotes: 0
Views: 799
Reputation: 2358
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<TextBox>)
{
txt.ReadOnly = true;
}
Upvotes: 0
Reputation: 1299
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 :)
Upvotes: 2
Reputation: 3504
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
Upvotes: 0
Reputation: 81721
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");
Upvotes: 0