Dynamically manipulate webcontrol element

Asked

Viewed 64 times

1

There is how to manipulate webcontrols name dynamically?

ex: I have 90 Textbox

textBox_01_name

textBox_02_name

textBox_03_name

textBox_04_name

...

today I have the following code

if (textBox_01_name.Text != Topo_DAraay[0].ValueDefaultSQL)
{

for each element. I thought I’d make a for and so go only changing the "textBox_" + i + "_name" Of course that doesn’t work but there is some way?

I can do it that way, but the code would be as big as the original

string campo = "textBox_" + i "_name";
TextBox teste1 = (TextBox)FindControl(campo);
if (teste1.Text != Topo_DAraay[0].ValueDefaultSQL)
{

Is there any way?

1 answer

1


You yourself almost answered the question. Using yourself FindControl can find a control in the context in which it is, you can then find the other controls, making a for and concatenating strings within it:

for (var i = 1; i <= 90; i++)
{
    string nomeControle = "textBox_" + i.ToString("00") "_name";
    TextBox txtBox = (TextBox)FindControl(nomeControle);
    if (txtBox.Text != Topo_DAraay[0].ValueDefaultSQL)
    {
        // sua lógica aqui!
    }
}

Would that be it? I do not understand why you say that this way would be as big as the original... it will be much smaller, because it will be able to apply the same logic to all controls, without replicating code.

  • yeah. that’s right.. I think I need a vacation......

  • Come on... it happens to me all the time! hehe =D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.