ASP.NET动态创建控件和回传 [英] ASP.NET dynamically created controls and Postback

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

问题描述

我知道这个问题已经被问上千次,和我以前用它挣扎着,但由于某些原因,我不能完成我想要完成的任务......点击时我有一个动态添加LinkBut​​ton的说将动态添加控制(在这个例子中,文本框),以相同的面板。这样做的目的是不断增加尽可能多的控件次被点击LinkBut​​ton的(即我点击了一次,一个盒子,然后再点击一下,就会给我2箱,再点击一下,增加了第3次)。在下面的code,我使用序列来为每个文本框的控制的唯一ID的当前日期和时间。

当我执行code,点击添加过滤器将产生一个新的文本框,但再次点击将创建一个新的,和其前一个处分的权利。相反,我想坚持的previous文本框,以及在其提交的任何数据。

您的帮助是AP preciated。

在ASPX:

 < ASP:面板ID =pnlFilter=服务器>< / ASP:面板>

在aspx.cs:

 保护无效Page_Init(对象发件人,EventArgs的发送)
{
        LinkBut​​ton的磅=新的LinkBut​​ton();
        lb.ID =lbAddFilter;
        pnlFilter.Controls.Add(磅);
        lb.Text =添加过滤器;
        lb.Click + =新的EventHandler(lbAddFilter_Click);
}
无效lbAddFilter_Click(对象发件人,EventArgs的发送)
{
    文本框TB =新的TextBox();
    tb.ID =TB+ DateTime.Now.ToBinary()的ToString()。
    pnlFilter.Controls.Add(TB);
}


解决方案

有关其他人试图做这样的事情:没有。相反,认为信息流和理解,有一个更好的方式来做到这一点。输入控制(多个)不需要被动态创建。它们可以是静态的,在填写并提交于一体,这些信息有去的地方(数据库,缓存,会话,例如)。一旦它的存在,对回传,通过您所选择的存储所有项目循环,并创建一个显示他们。

这是我做了什么,它使生活轻松了许多。希望它可以帮助别人。

I know this question has been asked thousands of times, and I've struggled with it before, but for some reason, I can't accomplish what I want to accomplish... I have a dynamically added LinkButton that when clicked will dynamically add a control (in this example, a textbox) to the same panel. The intent is to continuously add on as many controls as times the LinkButton was clicked (i.e. I click it once, one box, then another click will give me 2 boxes, another click adds a 3rd). In the code below, I use the current date and time serialized to create a unique ID for each textbox control.

When I execute the code, clicking "Add Filter" will generate a new textbox, but once clicked again will create a new one, and dispose of the one before it. Instead, I want to persist the previous textbox as well as any data submitted within it.

Your help is appreciated.

In the aspx:

<asp:Panel ID="pnlFilter" runat="server">

</asp:Panel>

In the aspx.cs:

protected void Page_Init(object sender, EventArgs e)
{
        LinkButton lb = new LinkButton();
        lb.ID = "lbAddFilter";
        pnlFilter.Controls.Add(lb);
        lb.Text = "Add Filter";
        lb.Click += new EventHandler(lbAddFilter_Click);
}


void lbAddFilter_Click(object sender, EventArgs e)
{
    TextBox tb = new TextBox();
    tb.ID = "tb" + DateTime.Now.ToBinary().ToString();
    pnlFilter.Controls.Add(tb);
}

解决方案

For anyone else trying to do something like this: don't. Instead, think of the flow of information and understand that there is a better way to do it. The input control(s) do not need to be dynamically created. They can be static, and upon filling out and submitting on one, that information has to go somewhere (database, cache, session, for example). Once its there, on postback, loop through all items in your storage of choice and create a display for them.

That is what I've done and it has made life a lot easier. Hope it helps someone.

这篇关于ASP.NET动态创建控件和回传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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