ASP.net Dynamically adding controls on Button press. Postback issue(ASP.net 在按钮按下时动态添加控件.回传问题)
问题描述
我有一个包含多个按钮的用户控件,根据按下的按钮不同的控件添加到页面(假设按钮 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).
我有以下代码:
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);
}
然后我有第三个按钮 (button3) 来获取页面上的所有控件及其值.(假设在这个例子中每个按钮只被点击一次).
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).
我的问题是当按下 button3 时,paramsCtrlDiv.controls 数组不包含已添加的控件.我知道我需要在每次回发的 Page_Load 时间添加这些控件.我的问题是我不知道用户添加了什么控件我不知道我想添加什么 Page_Load(可能有一个文本框,然后是标签,只是一个标签或一个 tb),我可以不控制用户按下什么.
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
推荐答案
因为您是动态执行此操作,所以您需要一种方法来存储您正在执行的操作,以便服务器可以使用每个 PostBack
重新创建它.如果您不想使用 Session
,请将数据存储在 ViewState
中(无论时间如何,它都会与页面一起保留).创建一个 List
并确保它是 Serializable
,然后将其存储在 ViewState
中.您可能希望存储控件类型、位置等,以便每次出现 PostBack
时都可以在 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 在按钮按下时动态添加控件.回传问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.net 在按钮按下时动态添加控件.回传问题
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01