【问题标题】:Trying to add a listbox and a checkbox items尝试添加列表框和复选框项目
【发布时间】:2014-01-19 00:14:08
【问题描述】:

当我尝试使用CheckBoxList1.SelectedItem.Value 时,它给了我错误。我有点困惑并浏览了这本 Murach ASP.Net 书,但我没有看到任何答案。我想要在我的 ListBox 中选择什么以及在我的 CheckBox 中选择什么并添加它们。有什么帮助吗?

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int ucost = int.Parse(DropDownList1.SelectedItem.Value);
        int ccost = int.Parse(//what does in here???);
        ttbox.Text = ccost.ToString();
    }

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

    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        ttbox.Text = "";
    }

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

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

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

这是一个更新。他们说一张照片值一千字。

【问题讨论】:

  • 你想从复选框列表中获取最后选择的项目吗?
  • 不,我想要的是,例如,假设用户点击了周五午餐的第一个复选框。它的值是 8。我想将 8 添加到我的列表框值的值上。

标签: c# asp.net checkbox listbox


【解决方案1】:

如果你想获得所有选中的复选框,你可以使用:

var selected = CheckBoxList1.Items.Cast<ListItem>().Where(x => x.Selected).ToList();

如果您只想获取选定的计数:

var count = CheckBoxList1.Items.Cast<ListItem>().Count(x => x.Selected)

如果要获取最后一个或第一个元素,可以使用FirstLast linq 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多