回发后动态创建的控件丢失数据

编程入门 行业动态 更新时间:2024-10-27 00:33:31
本文介绍了回发后动态创建的控件丢失数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

实际上,我在 Pageload 上创建 1 个 TextBox 并将该 TextBox 添加到 Panel.现在,我有一个 LinkBut​​ton,例如 Add Another.

Actually, I am Creating 1 TextBox on Pageload and adding that TextBox to Panel. Now, I have a LinkButton like Add Another.

我正在该 TextBox 中输入文本,如果需要,我需要通过单击 Add Another LinkBut​​ton 来创建新的 TextBox.

I am entering Text in that TextBox and if needed I need to Create New TextBox,by clicking Add Another LinkButton.

实际上,我可以获取计数并重新创建 TextBoxes.但是,问题是,我在以前生成的 Textboxes 中输入的文本丢失了.

Actually, I am able to get the count and recreate the TextBoxes. But,the Problem is that, My Entered text in the Previously Generated Textboxes is Missing.

任何人都可以给我一个解决方案吗?

Can Anyone,Suggest me a solution for this?

protected void Page_Load(object sender, EventArgs e) { try { if (!IsPostBack) { for (int i = 0; i < 5; i++) { TableRow row = new TableRow(); for (int j = 0; j < 5; j++) { TableCell cell = new TableCell(); TextBox tb = new TextBox(); tb.ID = "TextBoxRow_" + i + "Col_" + j; cell.Controls.Add(tb); row.Cells.Add(cell); } Table1.Rows.Add(row); } } } catch (Exception ex) { throw; } }

这是一个示例代码,同样的代码写在 Button_Click 也

This is a Sample Code, the same code is written in Button_Click Also

protected void ASPxButton1_Click(object sender, EventArgs e) { int k = Table1.Controls.Count; }

我在 Button_Click 上得到一个 Count=0.

I am getting a Count=0 on Button_Click.

推荐答案

您只需在每次回发期间在页面加载事件之前或之内重新实例化/重新初始化动态控件并将此控件添加到页面/表单/占位符.然后,通过父控件调用LoadPostData方法,将发布的数据自动分配给控件.

All you need to do is to re-instantiate / reinitialize dynamic controls before or within page load event each and every time during postback and add this control to page / forms / placeholders. Then, the posted data will automatically be assigned to the control by calling the LoadPostData method by the parent control.

查看文章以及如何编写动态控制代码 -如何维护动态控制事件,asp中回发期间的数据

check the article and how to write code for dynamic control - How to maintain dynamic control events, data during postback in asp

更多推荐

回发后动态创建的控件丢失数据

本文发布于:2023-10-12 20:47:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485857.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   动态   数据   回发后

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!