在运行时动态创建TextBox控件 [英] Create TextBox Control Dynamically at Runtime

查看:99
本文介绍了在运行时动态创建TextBox控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了用于动态生成文本框和标签的代码.请参阅下面的内容,但我没有得到textbox.text值.它显示类似
的错误 指定的参数超出有效值范围.参数名称:index
代码:

Hi I have written code for dynamic generation of textboxes and label.Refer below but i am not getting textbox.text values.It shows error like
Specified argument was out of the range of valid values.Parameter name: index
Code:

protected void ddlanswers_SelectedIndexChanged(object sender, EventArgs e)
        {
            int count = Convert.ToInt32(ddlanswers.SelectedValue.ToString());
            
            for (int i = 0; i < count; i++)
            {
                Label lbl = new Label();
                TextBox txt = new TextBox();
                lbl.Text = "Option " + (i + 1).ToString();
                lbl.ID = "Option" + (i + 1).ToString();
                
                txt.ID = i.ToString();
                TableRow row = new TableRow();
                TableCell cell = new TableCell();
                cell.Controls.Add(lbl);
                row.Cells.Add(cell);
                TableCell cell1 = new TableCell();
                cell1.Controls.Add(txt);
                row.Cells.Add(cell1);
                Table1.Rows.Add(row);            
         }           
        }
protected void btnsavequestion_Click(object sender, EventArgs e)
        {
            int count1 = Convert.ToInt32(ddlanswers.SelectedValue.ToString());             
            for (int i = 0; i < count1; i++)
            {
                TextBox tb = Table1.Rows[i].Cells[1].FindControl(i.ToString()) as TextBox;
     //I m getting error here
//error:Specified argument was out of the range of valid values.Parameter name: index           
                
            }
        }



[edit]已添加代码块,忽略HTML ..."选项已禁用-OriginalGriff [/edit]



[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]

推荐答案

您需要了解正在发生的事情:当然,问题在于"i"对于"Table1"中的行数越来越大,但是您需要查看"count1"的值并弄清楚为什么它大于行数. >
在行上放置一个断点
You need to find out what is going on: Certainly, the problem is that "i" is getting to big for the number of rows in "Table1", but you need to look at the value of "count1" and work out why it is bigger than the number of rows.

Put a breakpoint on the line
int count1 = Convert.ToInt32(ddlanswers.SelectedValue.ToString());

然后从那里开始.


这篇关于在运行时动态创建TextBox控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆