CheckBoxList not updating it's checked/not checked state

痞子三分冷 提交于 2019-12-25 04:35:29

问题


Hey, I have a CheckBoxList that gets populated with the database. When I make any changes to the state (check or uncheck) of a single checkbox, it doesn't get returned when I submit my form.

To its simplest form, I have:

<asp:CheckBoxList runat="server" ID="listEmployes" RepeatDirection="Horizontal">
</asp:CheckBoxList>

C#:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    _connection.Open();

    var employes = listEmployes.Items;

    foreach (ListItem employe in employes)
    {
        if (employe.Selected)
        {
            _command = new MySqlCommand(String.Format("INSERT IGNORE INTO Liste_Employes (Projet_ID, User_ID) VALUES ({0}, {1})", _projetId, employe.Value), _connection);
        }
        else
        {
            _command = new MySqlCommand(String.Format("DELETE IGNORE FROM Liste_Employes WHERE Projet_ID = {0} AND User_ID = {1}", _projetId, employe.Value), _connection);
        }

        _command.ExecuteNonQuery();
    }


}

Am I missing something? Thanks.


回答1:


Make sure your CheckBoxList is not being rebound on postbacks (if you're databinding from code).

If(!Page.IsPostBack) 
{
    // Bind code
}



回答2:


What is in your Page_Load method? I am guessing that you are not checking for !Page.IsPostBack before you bind the checkboxes.



来源:https://stackoverflow.com/questions/1776348/checkboxlist-not-updating-its-checked-not-checked-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!