且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从集合中间删除动态创建的控件

更新时间:2023-10-12 22:34:10

在开发 ASP.NET Forms 应用程序时,只要您在 ASP 的上下文中操作页面加载回传之间的.NET Forms框架和对象你会很安全.尽管如此,有时不添加自定义代码是具有挑战性的.在后面的代码中处理它是***的地方,用于动态地向页面添加控件.

当您删除控件时,ViewState应该会自动更新,因为您在调用ASP.NET Forms时直接在控件上使用ASP.NET Forms方法代码>parent.Controls.Remove("ID").如果您自定义命名控件,则需要自己提供所有 ID 或名称,以便它们在页面中是不同的.所以在这里我假设您已自定义命名控件,并且知道为 "ID" 真正放置什么.

就编码而言,ViewState 通常对您是透明的.有时您可能会发现自己在两次回传之间手动清除 ViewState(就像带有密码的表单,每次访问页面时都需要清除值).ViewState 也可以关闭off,所以想一想您的应用是否需要 ViewState 的额外开销.在动态表单上,我会尽力在后面的代码中实现向自定义数据源添加控件或数据绑定.原因是注册客户端脚本,所以 ASP.NET Forms 框架知道脚本,并且在客户端处理自定义 id 属性或命名方案可能很难从绑定 立场.

我亲自将 ASP.NET Forms 应用程序与 AngularJS 1.6 集成,虽然它比 ASP.NET MVC 更困难,并且需要更多配置strong>,它仍然是可能的,并为我提供了一种很好的方法来构建 ASP.NET 的动态表单.但是,在后面的代码中绑定回您的表单值需要从 Request.Form Collection 中查找和获取值.如果以这种方式完成,当您的表单位于 ASP.NET 外部时,这些对象将没有 ViewState.

这是否会更新 ViewState 以便我可以将我的计数器减 1 并将剩余控件从 0 重新编号为 3,或者我是否需要跟踪删除了哪个控件并保留编号 0、1、3、4?


就您在这里提到的策略而言,代码示例将是最有帮助的.我必须在那里做出很多假设来猜测可能会发生什么.我可以告诉你这没关系,但你仍然可能遗漏一些影响你个人结果的结果.删除一行时,它会自动更新ViewState.至于跟踪您如何使用不同值命名控件 Id 的编号方案,我个人不会在数字中留下间隙,因此请坚持使用 0 到 3,它应该简化您的代码,您不必在后面的代码中跟踪太多,否则它不会影响您的渲染逻辑.通过这种方式,控件在 post-back 期间唯一地绑定回您后面的​​代码,您可以轻松地遍历控件集合,而不必担心命名方案.

Maybe I'm borrowing trouble, but I have wrapped my head around dynamically creating controls. I am building a form on the fly that allows the user to enter as many rows of data as they wish.

I have methods for creating the controls based on a button click and am aware that as long as my controls have the same ID on each PostBack then the ViewState will populate them with the data they had before the PostBack.

My question is: Say the user has added 5 rows and filled them out. The decides to delete row 3. The easiest approach is to just call parent.Controls.Remove("ID"). Does this update the ViewState so that I can just decrement my counter by 1 and renumber the remaining controls from 0 to 3 or do I need to keep track of which one was deleted and keep the numbering 0, 1, 3, 4?

Do do I have to do something to clear the ViewState's record of the deleted control?

When developing an ASP.NET Forms application, as long as you operate within the context of the ASP.NET Forms framework and objects between page load and post-backs you will be safe. Albeit, it is sometimes challenging to do without adding custom code. Handling that in the code behind is exactly the best place for this, for adding controls dynamically to a page.

When you remove the control, ViewState should be updated automatically as you are using a ASP.NET Forms method directly on a control when you call parent.Controls.Remove("ID"). In the case you are custom naming the controls, you will need to provide all the id's or names yourself so that they are distinct for the page. So here I am under the assumption you have custom named the control, and know what to really put in place for "ID".

ViewState is generally transparent to you as far as coding is concerned. Sometimes you may find yourself manually clearing ViewState between post-backs (like for a form with a password, when every visit to the page you need to clear the value). ViewState can also be turned off, so think about whether or not your app needs the extra overhead for ViewState. On dynamic forms, I would do my best to implement adding controls or data binding to custom data sources in the code behind. The reason being is registering client-side scripts, so ASP.NET Forms framework knows about the scripts, and handling a custom id attribute or naming schemes client-side can be hard to debug or deal with from a binding standpoint.

I have personally integrated an ASP.NET Forms application with AngularJS 1.6, and while it was more difficult, and needed more configuration than with ASP.NET MVC, it was still possible and offered me a nice way to build dynamic forms out of ASP.NET's hands. However, binding back to your form values in the code behind requires looking and fetching values out of Request.Form Collection. If done in this manner, where your form is external to ASP.NET there will be no ViewState for those objects.

Does this update the ViewState so that I can just decrement my counter by 1 and renumber the remaining controls from 0 to 3 or do I need to keep track of which one was deleted and keep the numbering 0, 1, 3, 4?


As far as the strategy you are mentioning here a code sample would be most helpful. I have to make a lot of assumptions there to guess what might be happening. I could tell you it's okay, but you could still be missing something effecting the outcome in your personal results. When deleting a row, it will update the ViewState automatically. As far as keeping track of your numbering scheme for how you name your control Id's with distinct values, I would personally not leave gap's in the numbers, so stick to 0 to 3, and it should simplify your code and you will not have to track as much in the code behind or it will not effect your rendering logic. This way the controls bind uniquely back to your code behind during a post-back, and you can just iterate easily through the control collections, without worrying about naming schemes as much.