sheetal
sheetal

Reputation: 21

to convert string into TextBox control type

to convert string in to TextBox type i used following code

protected void txtNumber_TextChanged(object sender, EventArgs e)
{
    int num = Convert.ToInt16(txtNumber.Text);
    for (int i = 1; i <= num; i++)
    {
        String a = System.String.Concat("TextBox", i);

        TextBox dt1 =(TextBox)(this.FindControl(a);


    }
}

but in dt1 value remains null instead of 'a'.

Upvotes: 2

Views: 1489

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499760

Chances are you don't have a textbox with exactly the right ID then.

But it would be better to use an array or a list to start with... then you could use:

for (int i = 0; i < num; i++)
{
    TextBox tb = textBoxes[i];
    // Use tb
}

There's no point in looking something up by a string when you've actually got a sequence of consecutive numbers... that's exactly the use case for arrays and lists.

Also note that this code:

String a = System.String.Concat("TextBox", i);

is rather more simply written as:

string a = "TextBox" + i;

Upvotes: 1

Related Questions