只有一个选项是预先检查的而不是几个

时间:2015-09-29 16:52:37

标签: c# sql asp.net ado.net checkboxlist

我正在开发一个从多个SQL表中提取数据的项目。当我单击要编辑的行时,将填充整个复选框列表,但问题是我需要预先检查与该行有关的复选框。因此,当我添加一行时,我会检查几个复选框,但是当我编辑刚添加的新行时,只预选了一个复选框。我已经检查了SQL表,所有选项都在那里。 这是我的ASP.NET代码:

    <div>
    <div style="display: inline-block; padding-left: 80px;">
        <div>
            <h3>Available Entities</h3>
        </div>
        <div style="overflow-y: scroll; height: 500px;">
            <asp:CheckBoxList ID="cbAvailableEntities" runat="server"></asp:CheckBoxList>
        </div>
    </div>
    <div style="float: right; display: block; margin-right: 150px;">
        <asp:Button ID="btnSaveProject" runat="server" Text="Save Project" OnClick="btnSaveProject_Click" />
        <asp:Button ID="btnClose" runat="server" Text="Close" OnClick="btnClose_Click" />
    </div>
</div>

这是我的C#代码:

        private void PopulateProjectEntities()
    {
        using (SqlConnection conn = new SqlConnection(connString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "SELECT * FROM [dbo].Corporation WHERE IsEntity <> 0 ORDER BY CompanyName";
                cmd.Connection = conn;
                conn.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        ListItem _listProjectEntities = new ListItem();
                        _listProjectEntities.Text = sdr["CompanyName"].ToString();
                        _listProjectEntities.Value = sdr["CorpId"].ToString();
                        cbAvailableEntities.Items.Add(_listProjectEntities);
                    }
                }
                conn.Close();
            }
        }
    }


    public void GetSelectedEntities()
    {

        //create the connection and command objects
        SqlConnection connection = new SqlConnection(connString);
        SqlCommand command = new SqlCommand();
        //populate the command object
        command.Connection = connection;
        command.CommandText = "SELECT a.CorpID, a.CompanyName FROM [dbo].[Corporation] a Left join [dbo].[ProjectCovEntity] b on a.CorpID = b.CorpID WHERE b.ProjectID = " + _pID;

        using (connection)
        {
            connection.Open();
            using (SqlDataReader sdr = command.ExecuteReader())
            {
                while (sdr.Read())
                {
                    cbAvailableEntities.Items.FindByValue(sdr["CorpID"].ToString()).Selected = true;
                    ListItem _listPE = new ListItem();
                    _listPE.Text = sdr["CompanyName"].ToString();
                    _listPE.Value = sdr["CorpID"].ToString();
                    cbAvailableEntities.Items.Add(_listPE);
                }
            }
        }
    }

我已经坚持了几个星期了,我为此做了很多研究。我甚至为此查找过youtube视频。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

while (sdr.Read()) 
{
    ListItem currentCheckBox = cbAvailableEntities.Items.FindByValue(sdr["CorpID"].ToString());
    if (currentCheckBox != null) 
    {
        currentCheckBox.Selected = true;
    }
}

在另一篇文章中找到了这个答案。