存储控件并将控件动态添加到现有表单的最佳方法是什么
我有一个显示默认控件的通用表单。我正在尝试构建一个表单队列,如果我们有一个客户端需要额外的数据,我想存储捕获每个客户端数据所需的 asp.net 控件,并将它们与默认表单一起动态加载。
有谁知道或指导我一个架构/解决方案来实现这个[存储表单并加载并将它们动态添加到现有表单并从中检索传递的数据]。
我最初的想法是为每个需要比现有静态表单数据更多数据的客户端使用 xml 文件,并根据文件名(与客户端名称匹配)加载此 xml 并将其添加到内容占位符,然后循环浏览页面单击按钮可从这些动态字段中获取数据。
I have a generic form which shows default controls. I'm trying to build a form queue where if we have a client that wants extra data, I would like to store asp.net controls required to capture data for each client and load them dynamically along with default form.
Does anybody know or direct me to a architecture/solution to implement this[store the forms and load and add them dynamically to existing form and retrieve data from them passed].
My initial thought was to use xml file for each client that wants extra data than existing static form data and load this xml based on file name(which matches client name) and add it to a content-place-holder and then loop through page on button click to get data from those dynamic fields.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此 XML 文件适用于此示例:
首先在页面上配置 XmlDataSource。如果您计划将表单定义与客户一起存储在数据库中而不是 XML 文件中,则可以将其替换为 SqlDataSource。
在本例中,我们将使用一个 XML 文件,对于每个客户,都有一个节点,对于每种类型的表单,都有另一个节点。在此示例中,IBM 是客户,而 Employee 是表单。当然,XPath 属性需要动态设置:
现在您可以使用 ListView 数据绑定表单的一部分,使用 XML 中的属性为每个字段显示 TextBox 控件:
在后面的代码中,您现在可以检索以下值:每个字段并使用 data-field-id 属性将它们映射回来:
当然,当您还想将数据加载到编辑表单的字段中时,它会变得更加复杂,但这只是基于您自己的建议的通用方法。
存储时需要考虑的一些事情是映射到一个特殊的表,其中包含每种数据类型(字符串、数字、日期等)的列。为此,您需要使用数据类型来注释 XML,并根据该类型验证输入。
如果您不需要,那么您可以考虑将所有值序列化为单个字符串(JSON?),以便存储在数据库中的单个列中。但这使得几乎不可能对数据运行查询。
This XML file could work for this example:
Start off by configuring an XmlDataSource on the page. You can swap this for an SqlDataSource if you plan to store the form definition in the database along with the customer instead of in XML files.
In this case we would use one XML file, for each customer there would be a node, and for each type of form another node. In this example IBM is the customer, and Employee is the form. Of course the XPath attribute needs to be set dynamically:
Now you can data bind part of the form using a ListView, using the attributes in the XML to display a TextBox control for each field:
In your code behind you can now retrieve the values for each field and map them back using the data-field-id attribute:
Of course, it becomes more complex when you also want to load data into the fields for edit forms, but this is just a general approach based on your own suggestion.
Some things to consider for storage are mapping to a special table with columns for each type of data (strings, numbers, dates etc.). For that, you would need to annotate your XML with the type of data, and validate input based on that.
If you don't need that, then you could consider serializing all the values to a single string (JSON?) for storing in a single column in the database. This makes it almost impossible to run queries on the data though.
将所有控件添加到窗体中。隐藏可选的并根据需要显示它们。
Add all the controls to the form. Hide the optional ones and show them as required.
我建议使用绑定,动态创建用户控件并将子控件绑定到队列的数据(带有绑定源),并在回发后从绑定源获取数据并在队列中更新。
I suggest use binding, dynamicly create your user control and bind sub control to a data of your queue(with binding source), and after post-back get data from binding source and update in queue.