Get checkbox value and store in IList

453 Views Asked by At

Need some help since I'm still a newbie in programming. I have a gridview that has five columns, the three columns composed of checkboxes. Then I need to get these checkboxes value if they were checked and put it in IList. My problem is how to implement it when adding it to my IList. Please help. See my code below. I know something is wrong.

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

There are 2 best solutions below

0
On

Try this:

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

I assume that you want to add title prperty on the basis of checkbox or checked or not

you can user ternary operator for this ? :

i think you need this

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;            
    }                  
}