动态创建的控件或隐藏或显示的静态控件

发布于 2024-09-27 23:07:04 字数 428 浏览 4 评论 0原文

我有一组 ASP.Net 页面,根据配置文件中的条目数显示许多 asp:TextBox 字段。我知道字段的数量不会超过 10 个左右。鉴于此,我应该在标记中声明足够多的文本框,还是应该在代码隐藏中动态创建文本框?

每种方法都有优点和缺点,这就是我难以选择的原因。动态方法的优点是应用程序更加灵活 - 即使字段数量超过 10 个,我的应用程序也能够扩展。缺点是我现在混合了标记和逻辑 - 我的应用程序正在将文本框和文字(用于标签)插入页面中。这将使将来的维护变得更加困难,因为并非所有字段都位于 .aspx 文件中。

声明一个大表单然后只显示或隐藏必要的字段的优点是它可以将逻辑和标记分开。当然,缺点是我失去了灵活性。如果字段的数量超出了我预期的数量(并且存在很小的风险),我必须重新访问应用程序以添加更多字段。

那么,StackOverflow,你会选择哪个?更重要的是,你为什么选择你的方法?

I've got a set of ASP.Net pages that display a number of asp:TextBox fields depending on the number of entries in a configuration file. I know that the number of fields won't be going above 10 or so. Given that, should I declare a sufficiently large number of text boxes in markup, or should I dynamically create the textboxes in the code-behind?

There are advantages and disadvantages to each approach, which is why I'm having trouble choosing. The advantage to the dynamic approach is that the application is more flexible - even if the number of fields goes above 10, my application will be able to scale. The disadvantage is that I'm now mixing markup and logic - my application is inserting textboxes and literals (for the labels) into the page. This will make future maintenance harder because not all of the fields are in the .aspx file.

The advantage to declaring a large form and then just showing or hiding the necessary fields is that it keeps logic and markup separate. The disadvantage, of course, is that I lose flexibility. If the number of fields goes beyond the amount I anticipate (and there is a small risk of that) I have to revisit the application to add more fields.

So, StackOverflow, which would you choose? More importantly, why would you choose your approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2024-10-04 23:07:04

一种选择是将两全其美结合起来。如果将文本框放入转发器中,则可以动态控制显示的转发器行数,同时静态声明控件。

One option is to combine the best of both worlds. If you put your textbox in a repeater, you can then dynamically control how many repeater rows are displayed, while having the controls statically declared.

盗心人 2024-10-04 23:07:04

我肯定会采用动态方法,因为正如您所说,如果将来需求发生变化,这将会扩展,并且您不会向浏览器发送不必要的标记。
我不明白使用静态字段如何避免混合逻辑和标记的问题,因为您需要使用代码来隐藏不需要的控件。

I would definitely go with the dynamic approach because, as you say, this will scale if requirements change in the future and you will not be sending unnecessary markup to the browser.
I don't see how using static fields avoids the issue of mixing logic and markup as you will need to use code to hide the unwanted controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文