开发者

to convert string into TextBox control type

to convert string in to TextBox type i used following code

protected void txtNumber_TextChang开发者_StackOverflow中文版ed(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'.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜