ASP.net动态添加按钮上preSS控制。回传问题 [英] ASP.net Dynamically adding controls on Button press. Postback issue

查看:110
本文介绍了ASP.net动态添加按钮上preSS控制。回传问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几个按钮,根据按钮pressed不同的控件添加到页面的用户控件(可以说按钮1增加了一个文本框,按钮2添加了标签)。

I have a user control which contains several buttons, depending on the button pressed a different control is added to the page (lets say button 1 adds a TextBox, button2 adds a label).

我沿着线code:

protected void but1_click(object sender, EventArgs e)  
{  
    TextBox tb = new TextBox();  
    tb.ID = "tb1";  
    paramsCtrlDiv.Controls.Add(tb);  
}  

protected void but2_click(object sender, EventArgs e)  
{  
    Label lb = new Label();  
    lb.ID = "lb1";  
    paramsCtrlDiv.Controls.Add(lb);  
}  

然后我有一个第三个按钮(按钮3)获得页面和他们的价值观上的所有控件。 (假设每个按钮仅在这个例子中点击一次)。

I then have a third button (button3) to get all controls on the page and their values. (Assume each button is only clicked once for this example).

我的问题是,当按钮3为pressed的paramsCtrlDiv.controls数组不包含已添加的控件。我知道我需要在Page_Load中的时间在每次回发来添加这些控件。我的问题是,因为我不知道到底是什么控制着用户已添加,我不知道,我想添加什么样的Page_Load(有可能是一个文本框,然后标签,只是一个标签或只是一个TB),我可以'T控制哪些用户presses。

My problem is when button3 is pressed the paramsCtrlDiv.controls array doesn't contain the controls that have been added. I know I need to add these controls at Page_Load time on each postback. My issue is as I don't know exactly what controls the user has added I don't know what I want to add a Page_Load (there could be a text box, then label, just a label or just a tb), I can't control what the user presses.

我知道我可以存储会话中的一切,但我不知道这是一个完美的解决方案。此外,还可以在不同的选项卡这个控制的多个实例使每一个有正确地维护它自己的控件集合

I know I could store everything in the session but I'm not sure this is an elegant solution. There can also be several instances of this control on different tabs so each one has to correctly maintain it's own control collection

推荐答案

由于要动态这样做,你需要存储的一种方式你在做什么,因此,服务器可以与每个回发<重建它/ code>。如果你不希望使用会话,存储在的ViewState (将与页面坚持,无论数据的时间)。创建一个列表&LT; YourControlObjects&GT; ,并确保它是序列化然后将其存储在 ViewState的。你可能想存储的控制类型,位置等,这样就可以重建它在的Page_Load 每次有一个回传

Because you are doing this dynamically, you need a way of storing what your are doing so the server can recreate it with each PostBack. If you don't want to use Session, store the data in the ViewState (which will persist with the page regardless of time). Create a List<YourControlObjects> and make sure it is Serializable and then store it in the ViewState. You probably want to store the control type, location, etc. so that you can rebuild it on Page_Load each time there is a PostBack.

问题归结到你需要保持自己的状态,这些动态创建的控件。这只是一个建议,但你可以这样做许多不同的方式。

The problem comes down to you needing to maintain your own state for these dynamically created controls. This is just one suggestion but you could do this many different ways.

这篇关于ASP.net动态添加按钮上preSS控制。回传问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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