简体   繁体   English

在Web表单中重复信息-Asp.Net

[英]Repeating Information in a Web Form - Asp.Net

I am building an employment application form that submits data to a database for my business. 我正在构建一份就业申请表,该表将数据提交给我的公司的数据库。

In creating this form, one of the things that captured my attention is that when I created the section for employment history, I thought to myself that most applicants are going to have a variety of information to put in here, and making a required set of 3 past employers is far to rigid for the target prospect base. 在创建此表格时,引起我注意的一件事情是,当我创建就业历史部分时,我以为自己,大多数申请人都将在此处输入各种信息,并制定一套必要的信息。过去的3位雇主对于目标潜在客户群是一成不变的。 Some may have just 1 prior job, others may have 10 or more. 一些人可能只有一份以前的工作,其他人可能有十份或更多。 Plus, making a large amount of fields for this information not only looks bad to the eye, but it is also is a bad practice in the world of database development. 另外,为这些信息创建大量字段不仅看起来令人讨厌,而且在数据库开发领域中也是一种不好的做法。 SO... 所以...

What I would like to do, is something similar as I have done in an Access Database is the theory behind a Subform, where when a user begins to add input data, the form replicates itself making another set of datafields available for as many times as the user needs. 我想做的是,就像我在Access数据库中所做的一样,是子窗体背后的理论,在该窗体中,当用户开始添加输入数据时,该窗体进行自我复制,从而使另一组数据字段可用次数多达用户需求。 This way, only data is being stored - not empty space. 这样,仅存储数据,而不存储空白空间。

While this is rather a straightforward practice in Microsoft Access, I have discovered that Asp.Net web forms and SQL handle this process in an entirely different manner to accomplish the same goal. 尽管在Microsoft Access中这是相当简单的做法,但我发现Asp.Net Web表单和SQL以完全不同的方式处理此过程以实现相同的目标。

I have done some research on this topic and have discovered that there are many ways to go about this, and most things I found are very vague as to how to go about this. 我对此主题进行了一些研究,发现有很多方法可以解决此问题,而我发现的大多数事情对于如何解决都非常模糊。 So, I reach out to you. 因此,我与您联系。

How would I add a set of repeating fields to handle input of prior work experience? 如何添加一组重复字段来处理以前的工作经验? For simplicity sake, lets say we have three fields: Employer, Supervisor, and Job. 为了简单起见,可以说我们有三个字段:“雇主”,“主管”和“工作”。 Lets also assume that the addition of these three fields would take place after the user clicks and add button. 我们还假设这三个字段的添加将在用户单击并添加按钮之后进行。 What would I need to do to in order to get those three fields to repeat as many times as necessary on my asp.net web form? 为了使这三个字段在asp.net Web表单上重复所需的次数,我需要做什么?

There a lots of options for repeating information in an ASP.NET page, here is an article that goes over a few of them: 在ASP.NET页中有很多用于重复信息的选项,下面是其中的一篇文章:

Data Repeater Controls in ASP.NET ASP.NET中的数据中继器控件

There is a legacy .NET application where I work, for which most UIs were built using Web Forms. 我工作的地方是.NET遗留应用程序,大多数UI都是使用Web窗体构建的。 I recently had request similar to yours for a UI in this application which I achieved using AngularJS. 最近,在我使用AngularJS实现的此应用程序中,我对UI的要求与您相似。

I give this answer, knowing that it is not the pure Web Forms answer, but it is an solution that can be implemented in your existing Web Form, so I think it is still valid. 我给出了这个答案,知道它不是纯粹的Web窗体答案,但是它可以在您现有的Web窗体中实现,所以我认为它仍然有效。

Build a controller (Controller, ApiController, or even an .ashx 'Generic Handler') for submitting JSON data that holds all the data applicable to this particular user submission action. 构建用于提交JSON数据的控制器(控制器,ApiController甚至是.ashx“通用处理程序”),该数据包含适用于此特定用户提交操作的所有数据。 Build the UI components in this Web Form page using pure html and AngularJS (google 'ng-repeat'. You will see tons of examples that can be applied to your situation). 使用纯html和AngularJS(google'ng-repeat')在此Web窗体页面中构建UI组件。您将看到大量可以应用于您的情况的示例。 Don't actually use the Web Forms markup this time around. 这次不要实际使用Web窗体标记。 Your repeatable elements should be bound to data in the front end. 您的可重复元素应绑定到前端的数据。 When the user clicks the plus button to add more work history, you add to that data and Angular repeats your desired block. 当用户单击加号按钮以添加更多工作历史记录时,您将添加到该数据中,并且Angular会重复您想要的块。

Hook your submit action for your new html form up to the controller you created (using an ng-submit directive on the form or an ng-click directive on some submit element). 将您的新html表单的提交动作挂接到您创建的控制器上(在表单上使用ng-submit指令或在某些Submit元素上使用ng-click指令)。 If you know how to work with data and you have followed along so far, you should have it from here. 如果您知道如何处理数据,并且到目前为止已经学到了,则应该从这里开始。 If you need some more help, I strongly encourage investigating REST APIs and AngularJS. 如果您需要更多帮助,我强烈建议您研究REST API和AngularJS。

The sooner you start to learn how to program the front end using more sophisticated tooling than Web Forms, and the sooner you learn how to submit data using pure web technologies (Ajax and JavaScript) then the sooner you will be able to alleviate yourself from the confines of the Web Forms framework. 您越早开始学习如何使用比Web Forms更复杂的工具对前端进行编程,并且越早学习如何使用纯Web技术(Ajax和JavaScript)提交数据,那么您就越早可以从Web窗体框架的范围。 Don't get me wrong, it does a lot of things awesomely, and helps those who don't know the web, program for the web. 不要误会我的意思,它可以做很多事情,并且可以帮助那些不懂网络的人为网络编程。 In the end, you will be better off when you at least know how to step outside of its confines when needed. 最终,当您至少知道如何在需要时走出局限时,您会变得更好。

一种解决方案是在网格中将新字段显示为表单,并且用户可以使用同一表单添加多行数据。类似(+)按钮的操作可以为您做到这一点,只需保存以前的值当用户要求一组新的字段时的表单。

First of all, your database will need to be structured to allow this. 首先,您的数据库将需要进行结构化以允许这样做。 ie A table to hold the Employee information and another table to hold the employment history. 即一个用于保存员工信息的表和另一个用于保存雇用历史的表。 The history table will need to have a foreign key back to the Employee table with a many-to-one relationship. 历史记录表将需要具有多对一关系的外键回到Employee表。

As for ASP.NET, you need to look up creating dynamic controls . 至于ASP.NET,您需要查找创建动态控件 You can present a single set of fields with a plus button to add additional ones and then save these in a row of their own in the database. 您可以使用加号按钮显示一组字段,以添加其他字段,然后将其保存在数据库中的一行中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM