在Asp.Net中动态创建TextBox [英] Dynamic Creation of TextBox in Asp.Net

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

问题描述

大家好,



我试图在WebForm上动态创建TextBox但是文本框在运行后没有显示在表单上。我使用断点测试但没有错误。相同的代码适用于创建Checkboxes。请帮助...



以下是代码:

Hi All,

I tried to create TextBox on a WebForm Dynamically but the textboxes is not displayed on the form after running. I tested with the use of breakpoint but there is no error . The same code works fine for creation of Checkboxes. Pls Help ...

Here is the code:

protected void btnItem_Click(object sender, EventArgs e)
       {
           Table tblTxtBox = new Table();

           for (int i = 0; i < 5; i++)
           {

               TableCell tblcell = new TableCell();

               TableRow tblrow = new TableRow();

               TextBox txtBox = new TextBox();

               txtBox.ID = "txtBox" + i;

               tblcell.Controls.Add(txtBox);

               tblrow.Cells.Add(tblcell);

               tblTxtBox.Rows.Add(tblrow);

           }

           Panel1.Controls.Add(tblTxtBox);
       }

推荐答案



检查这个没有动态创建按钮点击事件 [ ^ ]

无法从生成的文本框中获取值在动态表格中 [ ^ ]

最好的问候

M.Mitwalli
Hi
Check this not firing dynamically cretated button click event[^]
Unable to get values from textbox generated in dynamic tables[^]
Best Regards
M.Mitwalli


这绝不会要可靠。控件不适用于viewstate,也不会在页面刷新后继续存在。使用动态控件最好的选择是没有回发,并且使用AJAX处理发送数据。
This will never be reliable. The controls will not work with viewstate, nor will they survive a page refresh. Your best bet with dynamic controls is no postbacks, and to handle sending data back using AJAX.


更新:

rahkan''答案更准确。阅读他的答案。



在您的页面加载事件中,添加:

updated:
rahkan''s answer is more accurate. Read his answer.

at your page load event, add this:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
                
    }
    AddTextBox();
}

void AddTextBox()
{
    Table tblTxtBox = new Table();
    for (int i = 0; i < 5; i++)
    {
        TableCell tblcell = new TableCell();
        TableRow tblrow = new TableRow();
        TextBox txtBox = new TextBox();
        txtBox.ID = "txtBox" + i;
        tblcell.Controls.Add(txtBox);
        tblrow.Cells.Add(tblcell);
        tblTxtBox.Rows.Add(tblrow);
    }
    Panel1.Controls.Add(tblTxtBox);
}


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

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