获取复选框值并存储在IList中

时间:2013-09-06 13:01:43

标签: c# asp.net gridview checkbox ilist

需要一些帮助,因为我还是编程新手。我有一个有五列的gridview,三列由复选框组成。然后我需要获取这些复选框值,如果它们被检查并将其放在IList中。我的问题是如何在将其添加到IList时实现它。请帮忙。请参阅下面的代码。我知道有些事情是错的。

public IList<Title> Checkboxes
{
    get
    {
       CheckBox chkEPS;
       CheckBox chkIMDF;
       CheckBox chkPS;

        IList<Title> checkedList = new List<Title>();
        foreach (GridViewRow row in gvwTitles.Rows)
        {
            chkABC = (CheckBox)row.FindControl("chkABC");
            chkABCD = (CheckBox)row.FindControl("chkABCD");
            chkABCDE = (CheckBox)row.FindControl("chkABCDE");

            if ((chkABC.Checked) && (chkABCD.Checked) && (chkABCDE.Checked))
            {
                checkedList.Add(new Title(What will be the value));
              // how will I add the value, I am also considering what if the user check the chkABC checkbox, while the others were not checked??
            }                
        }            
           return checkedList;            
    }                  
}

public Title(int id, bool _isPocketSharing, bool _isPreventSplitting, bool _isMissingDataFile)

2 个答案:

答案 0 :(得分:1)

我假设您要根据复选框添加title属性,或者选中是否

您可以对此? :

使用三元运算符

我认为你需要这个

public IList<Title> Checkboxes
{
    get
    {
       CheckBox chkEPS;
       CheckBox chkIMDF;
       CheckBox chkPS;

        IList<Title> checkedList = new List<Title>();
        foreach (GridViewRow row in gvwTitles.Rows)
        {
            chkABC = (CheckBox)row.FindControl("chkABC");
            chkABCD = (CheckBox)row.FindControl("chkABCD");
            chkABCDE = (CheckBox)row.FindControl("chkABCDE");


            checkedList.Add(new Title(1 , chkABC.Checked ? true : false, chkABCD.Checked ? true : false, chkABCDE.Checked ? true : false));

        }            
           return checkedList;            
    }                  
}

答案 1 :(得分:0)

试试这个:

  checkedList.Add(new Title{name=1 ,name2=??,name3= ??, name4=??});//name,name1,name2,name..is suppose to your property of class "Title"