asp.net复选框列表未注册为已选中

时间:2016-04-17 23:12:43

标签: asp.net checkboxlist

我试图找到选中复选框的复选框列表的索引。

我尝试过以下代码:

for (int i = 0; i < attendance_CBL.Items.Count; i++)
        {
            if (attendance_CBL.Items[i].Selected)
            {

            }
            else
            {

            }
        }

但出于某种原因&#39; attendance_CBL.Items [i] .Selected&#39;总是返回false;即使他们在该指数中被选中。

这里是页面加载:

protected void Page_Load(object sender, EventArgs e)
    {
        database = DBConnection.getInstance();
        connection = database.getConnection();
        firstNames = new List<String>();
        lastNames = new List<String>();
        studentID = new List<String>();
        displayStudents();


    }

这是我填充复选框列表的地方:

private void displayStudents()
    {

        attendance_CBL.Items.Clear();

        string id = "SELECT StudentID FROM StudentsTutorialSessions WHERE TutorialName = '" + Session["subcourse"].ToString() + "'";
        string firstName = "";
        string lastName = "";
        string studentID = "";



        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand(id, connection);
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                string name = "SELECT FirstName, LastName, StudentID FROM TemporaryStudents WHERE StudentID = '" + reader["StudentID"].ToString() + "' UNION SELECT FirstName, LastName, StudentID FROM FullyRegisteredStudents WHERE StudentID = '" + reader["StudentID"].ToString() + "'";
                OleDbCommand command2 = new OleDbCommand(name, connection);
                OleDbDataReader reader2 = command2.ExecuteReader();
                while (reader2.Read())
                {
                    firstName = reader2["FirstName"].ToString();
                    lastName = reader2["LastName"].ToString();
                    studentID = reader2["StudentID"].ToString();
                    attendance_CBL.Items.Add(firstName + " " + lastName + " - " + studentID);
                    firstNames.Add(firstName);
                    lastNames.Add(lastName);
                    this.studentID.Add(studentID);
                }
            }
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            connection.Close();
        }
    }

0 个答案:

没有答案