ASP.NET DataList - 所有文本框的 JavaScript 验证

发布于 2024-07-20 07:39:08 字数 476 浏览 4 评论 0原文

设置简单:数据列表形式的购物车。

标题有两个按钮:更新数量和删除所选项目

每个 DataList 项目(除其他外)都有一个 id="txtQty" 的文本框,用于存储购物车项目的数量。

用户应该能够更改这些文本框中的值,然后单击标题中的“更新数量”按钮以一次性保存所有更改。

我已经完成了实际保存的逻辑,但现在我要返回添加一些额外的验证。 我希望从 UpdateQuantities 按钮的 OnClientClick 事件中调用单个 JavaScript 函数,然后该函数将遍历所有 DataListItems,找到 txtQty 文本框,并确保它是有效的数字输入。

我在找出解决这个问题的最佳方法时遇到了一些困难。 到目前为止,我唯一真正的想法是循环遍历表单元素并找到 id 与“txtQty”匹配的任何内容(因为 ASP.NET 自动重写 id),然后验证该特定元素。 这听起来不像是最好的解决方案。 还有更好的想法吗?

Simple setup: Shopping Cart in the form of a data list.

Header has two buttons: Update Quantities and Remove Selected Items

Each DataList Item has (among other things) a textbox with the id="txtQty" that stores the quantity of the shopping cart item.

The user should be able to change the values in these textboxes and then hit the Update Quantities button in the header to save all the changes at one time.

I have the logic done for the actual saving, but now I am going back to add some extra validation. I was hoping to call a single JavaScript function from the OnClientClick event of the UpdateQuantities button, which would then iterate through all the DataListItems, find the txtQty textbox, and make sure it is valid numerical input.

I am having a bit of difficulty figuring out the best way of going about this. So far my only real idea would be to loop through the form elements and find anything with an id that matches "txtQty" (since ASP.NET rewrites the id's automatically), and then validating that specific element. This doesn't sound like the best solution. Are there any better ideas floating around out there?

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

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

发布评论

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

评论(4

想念有你 2024-07-27 07:39:09

重复的ID确实不是什么好事。 您可以尝试为文本框分配一个类 - “验证数量”会很好,因为您可以应用多个类作为验证规则。

<input type="textbox" class="validate-quantity validate-min" />

这样您就可以遍历表单来查找验证规则,而不是专门针对一个文本框。

Duplicate ID's really aren't a good thing. You could try assigning a class to the textbox - "validate-quantity" would be good, because you can apply multiple classes as validation rules.

<input type="textbox" class="validate-quantity validate-min" />

That way you could iterate through the form looking for your validation rules, instead of targeting a specifically a textbox.

暖风昔人 2024-07-27 07:39:09

为什么不将验证添加为每个文本框的 onchange 事件的属性。 当他们做出更改时,您最好进行验证。

您可以添加验证控件或自行构建。

why wouldn't you just add the validation as an attribute on the onchange event of each of the textboxes. You're better off doing the validation when they make the change.

You can either add a validation control or roll your own.

苍风燃霜 2024-07-27 07:39:08

为什么不在 DataGrid 上放置一个验证器来检查数量是否是有效数字? 这将是迄今为止最简单的。

如果这不是解决方案,我会让所有这些 txtQty 文本框具有相同的 css 类。 然后,您可以使用 JQuery 查找具有该类名称的所有元素并循环遍历它们。 这比循环遍历整个表单并检查元素的 id 是否包含“txtQty”要好得多。

另一种方法是使用一个隐藏字段,其中包含要检查的文本框的所有 ID。 您将在添加文本框时添加到此隐藏字段。 然后只需分解 id 数组中的隐藏字段,并找到这些 id。

Why not put a validator on your DataGrid to check to see if the qty is a valid number? This would be the simplest by far.

If that isn't a solution, I would have all those txtQty textboxes have the same css class. Then you can use JQuery to find all the elements with that class name and loop through them. Which is far better than looping through the entire form and checking if the id of the element contains 'txtQty' in its id.

Another way, is to have a hidden field that would have all ids of the textboxes you want to check. You would add to this hidden field as the text boxes are added. Then just break out the hidden field in an id array, and find just those ids.

萤火眠眠 2024-07-27 07:39:08

我最终使用了一些 LongHorn 的方法来获得我想要的结果。 使用自定义验证器,我使用它的客户端验证功能来调用 JS 函数,该函数检查文本更改事件上的各个文本框。

这不是我理想的解决方案,但它很有用。 在进行任何处理之前,我还会对整个列表进行服务器端验证,以防万一。

I ended up using a bit of LongHorn's method to get my desired results. Using a custom validator I used the client-side validation ability of it to call a JS function which checked the individual textbox on text change event.

It wasn't my ideal solution, but it was useful. I also have the server-side validation occuring for the whole list before any processing occurs just in case as well.

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