如何检测清单框的项目是否被选中? [英] How to detect whether the items of checklist box is checked or not?

查看:83
本文介绍了如何检测清单框的项目是否被选中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果选中了复选框列表中的项目,我想添加整数.
反之亦然...
任何想法...

可以说..
书,笔,颜色共有三项
如果已检查帐簿的金额应为200,并且如果同时检查笔和帐簿的金额应为200 + 40 = 240.
如果未选中该书,则新金额应为40 ...

i want to add the integer if the items of the checklist box is checked.
and vice versa...
any idea...

lets say..
there are three items book, pen, color
if book checked the amount should be 200 and if both pen and book is checked 200+40=240 should be the new amount .
if book is unchecked then new amount should be 40...

推荐答案

您好,

你也可以试试这个

aspx标记:

Hello,

you can try this also

aspx markup:

<asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem Value="book">The Book</asp:ListItem>
            <asp:ListItem Value="pen">The Pen</asp:ListItem>
            <asp:ListItem Value="color">The Color</asp:ListItem>
        </asp:CheckBoxList>



背后的代码:



Code behind:

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int totalValue = 0;
            foreach (ListItem item in CheckBoxList1.Items)
            {
                if (item.Selected)
                {
                    Choices currentChoice = (Choices)Enum.Parse(typeof(Choices),item.Value.ToString());
                    totalValue += (int)currentChoice;
                }
            }
            Response.Write("Total Choices: " + totalValue.ToString());
        }
    }

    public enum Choices
    {
        book = 100,
        pen = 40,
        color = 60
    }




享受....

谢谢,
Hemnat




Enjoy....

Thanks,
Hemnat


自己尝试过什么吗?确实,这应该非常简单.如何为每个复选框添加一个if块的方法,该方法将与该复选框相关的数字简单地添加到总计中,然后返回总计.

例如

Tried anything yourself? This should be extremely simple really. How about a method with an if block for each check box that simply adds the number relevant to that check box to a total then returns the total.

e.g.

private int TotalCheckBoxes()
{
    int Total = 0;

    if([BookBox].Checked)
        Total += 200;

    if([PenBox].Checked)
        Total += 40;

    if([ColorBox].Checked)
        Total += 50;

    return Total;
}



甚至只是拥有一个与数组中每个复选框相对应的整数数组,然后枚举列表中的每个复选框,将数组中的相关数字添加到总数中.

真的,

Ed:)



Or even just have an array of ints that corresponds to each checkbox in your list then enumerate each checkbox in the list adding the relevant number from the array to a total.

Simply really,

Ed :)


我明白了...
现在我在列表框中有项目列表.
以及复选框中的相同项目.
当我在列表框中选择项目时,我希望在listchecked框中选中同一项目
任何想法...
我试过了
I got it...
now i have list of items in list box.
and same items in check list box.
as i select items in list box i want same item checked in listchecked box
any idea...
i tried
lstchk_box.Items[index].Select=ture;


但在c Sharp中不可用.
任何想法...


but is not available in c sharp.
any idea...


这篇关于如何检测清单框的项目是否被选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆