Handle webcontrol element dynamically

1

Is there a way 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 about doing a for and so go only by changing "textBox_" + i + "_name" Of course that does not work, but is there anyway?

I can do this, but the code would look 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?

    
asked by anonymous 01.04.2014 / 18:13

1 answer

1

You yourself almost answered the question. If you use FindControl you can find a control in the context it is in, you can then find the other controls, making 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 you can apply the same logic to all controls, without replicating code.

    
01.04.2014 / 18:29