如何从复选框列表中获取最新选定的值?

发布于 2024-09-18 08:16:54 字数 156 浏览 9 评论 0原文

我目前面临一个问题。如何从 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 技术交流群。

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

发布评论

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

评论(3

心安伴我暖 2024-09-25 08:16:54

如果我理解正确,这就是我要使用的代码:

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    int lastSelectedIndex = 0;
    string lastSelectedValue = string.Empty;

    foreach (ListItem listitem in CheckBoxList1.Items)
    {
        if (listitem.Selected)
        {
            int thisIndex = CheckBoxList1.Items.IndexOf(listitem);

            if (lastSelectedIndex < thisIndex)
            {
                lastSelectedIndex = thisIndex;
                lastSelectedValue = listitem.Value;
            }
        }
    }
}

是否有任何事件捕获系统可以帮助我识别生成事件的确切列表项?

您使用事件 CheckBoxList1_SelectedIndexChanged复选框列表。当单击列表中的复选框时,将调用此事件,然后您可以检查所需的任何条件。

编辑:

以下代码允许您获取用户选择的最后一个复选框索引。通过这些数据,您可以获得用户最后选择的值。

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string value = string.Empty;

    string result = Request.Form["__EVENTTARGET"];

    string[] checkedBox = result.Split('
); ;

    int index = int.Parse(checkedBox[checkedBox.Length - 1]);

    if (CheckBoxList1.Items[index].Selected)
    {
        value = CheckBoxList1.Items[index].Value;
    }
    else
    {

    }
}

If I understood it right, this is the code I'd use:

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    int lastSelectedIndex = 0;
    string lastSelectedValue = string.Empty;

    foreach (ListItem listitem in CheckBoxList1.Items)
    {
        if (listitem.Selected)
        {
            int thisIndex = CheckBoxList1.Items.IndexOf(listitem);

            if (lastSelectedIndex < thisIndex)
            {
                lastSelectedIndex = thisIndex;
                lastSelectedValue = listitem.Value;
            }
        }
    }
}

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.

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string value = string.Empty;

    string result = Request.Form["__EVENTTARGET"];

    string[] checkedBox = result.Split('
); ;

    int index = int.Parse(checkedBox[checkedBox.Length - 1]);

    if (CheckBoxList1.Items[index].Selected)
    {
        value = CheckBoxList1.Items[index].Value;
    }
    else
    {

    }
}
浅笑依然 2024-09-25 08:16:54

下面的代码为您提供了最新选定的 CheckBoxList 项目。

string result = Request.Form["__EVENTTARGET"];
string [] checkedBox = result.Split('

希望这有帮助。

); ; int index = int.Parse(checkedBox[checkedBox.Length - 1]); if (cbYears.Items[index].Selected) { //your logic } else { //your logic }

希望这有帮助。

Below is the code which gives you the Latest selected CheckBoxList Item.

string result = Request.Form["__EVENTTARGET"];
string [] checkedBox = result.Split('

Hope this helps.

); ; int index = int.Parse(checkedBox[checkedBox.Length - 1]); if (cbYears.Items[index].Selected) { //your logic } else { //your logic }

Hope this helps.

想挽留 2024-09-25 08:16:54

不了解您的情况,但作为用户,我不希望每次选中复选框项目时都会回发页面。

这是我要使用的解决方案(jQuery):

在表单上声明一个服务器端隐藏字段:

<asp:HiddenField ID="HiddenField1" runat="server" EnableViewState="true" />

然后为复选框连接客户端事件处理程序以存储单击的复选框:

$('.someclassforyourcheckboxes').click(function() {
   $('#HiddenField1').val($(this).attr('id'));

这是一种用于存储复选框 ID 的轻量级机制单击“最新”复选框。并且您不必为复选框设置 autopostback=true 并进行不必要的回发。

您不必使用 jQuery - 您可以使用常规 Javascript,但是,为什么要做更多工作呢? =)

然后,当您实际进行回发时(我假设在提交按钮上单击),只需检查隐藏字段的值。

当然,除非您想在每次单击复选框时进行回发,但我无法想象您想要这样做的场景(也许您正在使用 UpdatePanel)。

编辑

复选框列表的 HTML 如下所示:

<input type="checkbox" name="vehicle" value="Bike" /> I have a bike

因此,您可以访问三个内容:

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:

<asp:HiddenField ID="HiddenField1" runat="server" EnableViewState="true" />

Then wire up client-side event handlers for the checkboxes to store checkbox clicked:

$('.someclassforyourcheckboxes').click(function() {
   $('#HiddenField1').val($(this).attr('id'));

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:

<input type="checkbox" name="vehicle" value="Bike" /> I have a bike

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.

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