使用 jquery 动态生成字段并使它们在 asp.net 上的服务器端
您好,这似乎是一个奇怪的问题,但这是我的情况:
我有一个表单,用户可以单击并添加包含某些输入字段的行。这严格是在客户端使用 JQuery 完成的。我可以使用 Request.Form 和字段名称在 asp.net 端回发时访问输入值,这很好。问题是当页面回发时,字段明显消失。所以我无法进行错误验证或其他事情。我在这里可能的解决方案是什么?当回发发生时,我是否应该在服务器端动态生成相应的字段,并将已从客户端生成的字段中读取的值放入其中,以便对用户透明?这是一种迂回的做事方式吗?有更好的办法吗?
我正在考虑在服务器端添加一个 ajax 方法,该方法将返回验证信息,我可以使用它来发出错误信号或客户端上没有的信号,而无需回发,但导致回发的按钮是“下一个”按钮,如果没有错误,我需要它进入下一页。
我正在寻找其他人可能有的更多见解以及这些方法的优缺点。
谢谢
Hi this may seem like a weird question, but here's my situation:
I have a form where the user can click and add rows which contain certain input fields. This is strictly done on client side using JQuery. I can access the input values on postback on asp.net side using Request.Form and the name of the fields, which is fine. The problem is when the page posts back, the fields obviously disappear. So I have no way of doing error validation or other stuff. What are my possible solutions here? Should I dynamically generate the corresponding fields on server side when postback happens and put the already read in values from the client side generated fields so that it's transparent to users? Is that a round about way of doing things? Is there a better way?
I was thinking of adding an ajax method on the server side which would return back validation info and I could use that to signal errors or what not on client side without ever posting back, but the button that causes the post back is the "next" button, and I need it to go to the next page if there are no errors.
I'm looking for any more insights someone might have and pros and cons of the approaches.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要动态添加输入字段,则必须维护新添加字段的服务器端哈希,并在 Page_Init 中重新实例化它们,以便它们在回发之间持续存在。
我认为你的 AJAX 想法很好:
List;
。此方法可以提高您在客户端添加字段的速度,并防止动态字段在回发时消失。
If you want to add input fields dynamically, you will have to maintain a server-side hash of the newly added fields and re-instantiate them in Page_Init in order for them to persist between postbacks.
I think your AJAX idea is good:
List<String>
.This method gets you the speed of adding the fields on the client side, and keeps your dynamic fields from disappearing on postback.
1)快速而肮脏,您可以使用 UpdatePanel,但这会增加一些流量的大小。它允许您通过回发表的整个标记来包含验证控件。
2)您可以使用某种 CustomValidator 或自定义验证方法,只需使用 Request.Form 值。只要您有某种方法来识别字段,您就应该能够解决这个问题,但是如果回发失败,您将必须重新创建场景,因为您将丢失这些部分。
3) 您可以通过 WebMethod 调用完成整个“回发”并手动处理所有事情。这可能是最干净的方法,因为无论如何您都将传统的 WebForms 内容与更多客户端模型混合和匹配。
1) Quick and dirty, you could use an UpdatePanel, but that would increase the size of the traffic some. It would allow you to include validation controls by posting back the whole markup for the table.
2) You could use some kind of CustomValidator or custom validation method and just use the Request.Form values. As long as you have some way to identify the fields you should be able to work that out, but you'll have to recreate the scene if the postback fails since you'll lose those parts.
3) You could do the whole "postback" via a WebMethod call and handle everything manually. This is probably the cleanest way to do it since you are mixing and matching the traditional WebForms stuff with a more clientside model anyway.