使用带有模式对话框(jquery)的服务器端验证器?

发布于 2024-10-05 06:59:01 字数 712 浏览 0 评论 0原文

是否有一种简单的方法可以在模式对话框中显示的表单中使用 ASP.NET 验证控件的服务器端验证?我正在使用 jQuery 和 SimpleModal(在 C#、VS2010、.NET 4.0 中),

我有一个工作正常的模态表单。我需要使用服务器端验证,因为逻辑取决于正在访问的记录的特定数据

我现在正在处理的项目的解决方案是使用 jQuery ajax 调用将所有表单数据传递到服务器并获取在允许帖子继续进行之前返回验证结果。但这实现起来相对耗时,并且在某些情况下我现在处理的所有验证代码都已经存在。

第一个挑战是模式对话框当然会在完整回发时关闭。所以你可以在对话框中放置一个 UpdatePanel...甚至不用考虑太多,但我认为它不会很好地工作。事实并非如此。首先打开模式对话框的表单已经在 UpdatePanel 中,这进一步使事情变得混乱。

不管怎样,我尝试将模态表单的内容放入 UpdatePanel 中。它实际上确实进行了部分回发,对话框保持打开状态,但对话框的内容不会随着我更改服务器端的任何内容而更新。但是,如果我在测试验证代码后关闭并重新打开同一页面上的对话框,它的内容实际上会更新以反映这些更改。显然,对话框的呈现方式使 ASP.NET 感到困惑。或者反之亦然。但这从一开始就显得很粗略。

我并没有试图破解这个问题,而是希望其他人能提出一些关于更好的方法来解决这个问题的建议。或者只是告诉我,我正在努力混合苹果和橙子,如果这是唯一明智的做法,我应该将其保留在所有客户端(或客户端 + jQuery ajax)。

Is there a straightforward way to use server-side validation with ASP.NET's validation controls in a form that's displayed in a modal dialog? I am using jQuery and SimpleModal (in C#, VS2010, .NET 4.0)

I've got a modal form which works fine. I need to use a server-side validation because the logic depends on data specific to the record being accessed

My solution for the project I'm working on now is to use a jQuery ajax call to pass all the form data to the server and get back the validation results before allowing the post to proceed. But this is relatively time consuming to implement, and in some situations I'm dealing with now all the validation code exists already.

The first challenge is that of course the modal dialog will close on a full postback. So you could put an UpdatePanel inside the dialog... without even thinking about this too much, though, I assumed that it wouldn't work out that well. It doesn't. And the form which opens the modal dialog to begin with is already in an UpdatePanel, which further confuses matters.

Anyway, I tried putting the contents of the modal form in an UpdatePanel for the heck of it. It does actually do a partial postback, the dialog remains open, but the contents of the dialog do not get updated with anything I change server side. If I close and re-open the dialog on the same page after testing the validation code, though, its contents are in fact updated to reflect these changes. Obviously the way the dialog is rendered is confusing ASP.NET. Or vice-versa. But this just seems sketchy from the get-go.

Rather than trying to hack my way through this problem I was hoping that others had some suggestions about a better way to approach this. Or just tell me I'm trying to hard too mix apples and oranges and I should keep it all client side (or client side + jQuery ajax) if that is the only sensible thing to do.

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

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

发布评论

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

评论(1

醉生梦死 2024-10-12 06:59:01

我采取的两种方法:

1)通过ajax提交数据,响应包括成功:true或false。如果为 false,则会包含一条详细说明该问题的消息。

2)正常提交表格。如果存在验证问题,但错误出现在页面上的隐藏 div 中,请编写 JS 在页面加载时检查该 div 中的内容,并根据需要显示警告/错误。

The two approaches I've taken:

1) submit the data via ajax, the response includes a success:true or false. If false, there is a message included that details the issue.

2) submit the form normally. If there is a validation problem, but the errors in a hidden div on the page and write JS to check for content in that div when the page loads and display the warning/error as necessary.

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