将控件添加到表控制动态 [英] Adding controls to a table control dynamically

查看:111
本文介绍了将控件添加到表控制动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表控制表1

而在click事件添加的控件给它一个按钮为:

And added controls to it in click event of one button as :

protected void Button2_Click(object sender, EventArgs e)
{
    TableRow row;
    TableCell cell;
    for (int i = 0; i < 3; ++i)
    {
        TextBox txt = new TextBox();
        txt.Text = i.ToString();
        row = new TableRow();
        cell = new TableCell();
        cell.Controls.Add(txt);
        row.Controls.Add(cell);
        Table1.Controls.Add(row);
    }
}

但我不能在另一个按钮的点击事件检索该控件。我认为这是因为回传的。

but i cant retrieve this controls in click event of another button. i think it is because of postback.

如何prevent呢?

How can i prevent it?

推荐答案

在添加控件动态,如果你想访问他们在回发,则需要重新创建它们的每次的页面是加载。

When adding controls dynamically, if you want to access them on postback, you need to re-create them every time the page is loaded.

在一个按钮的点击处理程序添加他们,期待他们在那里为其他按钮将无法正常工作的点击处理程序,因为他们没有第二回传重新创建。

Adding them in the click handler of one button and expecting them to be there for the click handler of another button will not work, as they have not been re-created on the second postback.

您需要了解 asp.net页面生命周期和改变你的网页以配合它的逻辑。

You need to understand the asp.net page life cycle and change the logic of your page to fit in with it.

这篇关于将控件添加到表控制动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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