如何从复选框列表中获取最新选定的值?
我目前面临一个问题。如何从 asp.net 复选框列表中获取最新选定的值?
通过循环遍历复选框列表的项目,我可以获得最高的选定索引及其值,但预计用户不会从较低到较高的索引顺序选择复选框。那么,该如何处理呢?
是否有任何事件捕获系统可以帮助我识别生成事件的确切列表项?
I am currently facing a problem. How to get the latest selected value from a asp.net checkbox list?
From looping through the Items of a checkbox list, I can get the highest selected index and its value, but it is not expected that the user will select the checkbox sequentially from lower to higher index. So, how to handle that?
Is there any event capturing system that will help me to identify the exact list item which generates the event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解正确,这就是我要使用的代码:
是否有任何事件捕获系统可以帮助我识别生成事件的确切列表项?
您使用事件 CheckBoxList1_SelectedIndexChanged复选框列表。当单击列表中的复选框时,将调用此事件,然后您可以检查所需的任何条件。
编辑:
以下代码允许您获取用户选择的最后一个复选框索引。通过这些数据,您可以获得用户最后选择的值。
If I understood it right, this is the code I'd use:
Is there any event capturing system that will help me to identify the exact list item which generates the event?
You use the event CheckBoxList1_SelectedIndexChanged of the CheckBoxList. When a CheckBox of the list is clicked this event is called and then you can check whatever condition you want.
Edit:
The following code allows you to get the last checkbox index that the user selected. With this data you can get the last selected value by the user.
下面的代码为您提供了最新选定的 CheckBoxList 项目。
希望这有帮助。
Below is the code which gives you the Latest selected CheckBoxList Item.
Hope this helps.
不了解您的情况,但作为用户,我不希望每次选中复选框项目时都会回发页面。
这是我要使用的解决方案(jQuery):
在表单上声明一个服务器端隐藏字段:
然后为复选框连接客户端事件处理程序以存储单击的复选框:
这是一种用于存储复选框 ID 的轻量级机制单击“最新”复选框。并且您不必为复选框设置 autopostback=true 并进行不必要的回发。
您不必使用 jQuery - 您可以使用常规 Javascript,但是,为什么要做更多工作呢? =)
然后,当您实际进行回发时(我假设在提交按钮上单击),只需检查隐藏字段的值。
当然,除非您想在每次单击复选框时进行回发,但我无法想象您想要这样做的场景(也许您正在使用 UpdatePanel)。
编辑
复选框列表的 HTML 如下所示:
因此,您可以访问三个内容:
Vehicle =
$(this).attr('name');
自行车 =
$(this).attr('value');
我有一辆自行车 =
$(this ).html();
如果您尝试访问数据绑定值,请尝试第二种技术。
尝试一下。
Don't know about you, but as a user i wouldn't want the page to post back every time a checkbox item was checked.
This is the solution i would go with (jQuery):
Declare a server-side hidden field on your form:
Then wire up client-side event handlers for the checkboxes to store checkbox clicked:
This is a lightweight mechanism for storing the ID of the "latest" checkbox clicked. And you won't have to set autopostback=true for the checkboxes and do an unecessary postback.
You dont HAVE to use jQuery - you can use regular Javascript, but, why do more work? =)
Then when you actually do the postback (on a submit button click i assume), just check the value of the hidden field.
Unless of course you WANT to postback on every checkbox click, but i can't envision a scenario in which you'd want this (maybe you're using UpdatePanel).
EDIT
The HTML of a checkbox list looks like this:
So, you can access three things:
Vehicle =
$(this).attr('name');
Bike =
$(this).attr('value');
I have a bike =
$(this).html();
If you're trying to access the databound value, try the second technique.
Give that a try.