有什么办法只在 CheckBoxList 中获取选定的索引吗?

发布于 2024-10-14 19:45:18 字数 1098 浏览 1 评论 0原文

我只想在 checkBox lsit 中获取选定的索引或项目,而不是像 ListBox 中的 Like 那样迭代每个项目。

我在两种情况下获取选定的值通过这种方式:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ChkBxList_2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        string li = "";
        foreach(ListItem lt in CheckBoxList1.Items)
        {
            if(lt.Selected)
            li += lt.Text;

        }
        Response.Write(li);


    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        string li = "";
        foreach (int lt in ListBox1.GetSelectedIndices())
        {
          li += ListBox1.Items[lt].Text;

        }
        Response.Write(li);


    }
}

在列表框中,我们可以选择仅获取查看的项目是否有复选框列表?

I want to get the selected Indices or Items only in checkBox lsit instead of iterating through each item as Like is there in ListBox.

I am Getting Selected Value In tWo case In this way:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ChkBxList_2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        string li = "";
        foreach(ListItem lt in CheckBoxList1.Items)
        {
            if(lt.Selected)
            li += lt.Text;

        }
        Response.Write(li);


    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        string li = "";
        foreach (int lt in ListBox1.GetSelectedIndices())
        {
          li += ListBox1.Items[lt].Text;

        }
        Response.Write(li);


    }
}

In ListBox we have the Option To get Seected Only Items is there any For Check Box List?

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

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

发布评论

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

评论(2

冬天旳寂寞 2024-10-21 19:45:18

我认为不存在,但您可以使用这个扩展方法来实现这一点:

public List<ListItem> GetSelectedItems(this CheckBoxList checkBoxList)
{
    List<ListItem> list = new List<ListItem>();
    foreach(ListItem lt in checkBoxList)
    {
        if(lt.Selected)
            list.Add(lt);
    }
    return list;
}


//Call it like this
checkBoxList.GetSelectedItems();

I dont think there is but you could use this extension method that does exactly that:

public List<ListItem> GetSelectedItems(this CheckBoxList checkBoxList)
{
    List<ListItem> list = new List<ListItem>();
    foreach(ListItem lt in checkBoxList)
    {
        if(lt.Selected)
            list.Add(lt);
    }
    return list;
}


//Call it like this
checkBoxList.GetSelectedItems();
找回味觉 2024-10-21 19:45:18

你实际上回答了你自己的问题。与 ListBox 控件不同,无法获取 CheckBoxList 控件中仅选定的项目。

本文有一个解释和解决方法帮助。

http://weblogs.asp.net/jgalloway/archive/ 2005/10/02/426346.aspx

You actually answered your own question. There is no way to get the selected only items in the CheckBoxList control, unlike the ListBox control.

This article has an explanation and a work around help method.

http://weblogs.asp.net/jgalloway/archive/2005/10/02/426346.aspx

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