Nirmal
Nirmal

Reputation:

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

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

Upvotes: 0

Views: 799

Answers (5)

fARcRY
fARcRY

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

Neil
Neil

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

Matt Hanson
Matt Hanson

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

Tarik
Tarik

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

Newbie
Newbie

Reputation:

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

Upvotes: 0

Related Questions