gymcode
gymcode

Reputation: 4623

ASP.NET OnCheckChange does not work

I am using VS2005 C#.

I have 2 checkboxes, both indicates the role of Administrator and User.

I have implemented OnCheckChange on both the checkboxes.

However, when I change the check status of them, nothing runs.

May I know what did I missed out?

Below are the code snippets for the checkboxes:

<asp:CheckBox ID="adminCB" runat="server" Text="Administrator" OnCheckedChanged="Admin_CC" /><br />
<asp:CheckBox ID="userCB" runat="server" Text="User" OnCheckedChanged="User_CC" /> </td>

Code for Admin_CC:

public void Admin_CC(Object sender, EventArgs e)
{
    if (adminCB.Checked == true)
    {
        string[] newusers = new string[UsersListBox3.GetSelectedIndices().Length];

        for (int i = 0; i < newusers.Length; i++)
        {
            newusers[i] = UsersListBox3.Items[UsersListBox3.GetSelectedIndices()[i]].Value;
        }

        Roles.AddUsersToRole(newusers, "Administrator");

        // Re-bind users in role to GridView.

        usersInRole = Roles.GetUsersInRole(RolesListBox.SelectedItem.Value);
        UsersInRoleGrid.DataSource = usersInRole;
        UsersInRoleGrid.DataBind();

    }
    if (adminCB.Checked == false)
    {

        Roles.RemoveUserFromRole(UsersListBox3.SelectedItem.Value, "Administrator");

        // Re-bind users in role to GridView.

        usersInRole = Roles.GetUsersInRole(RolesListBox.SelectedItem.Value);
        UsersInRoleGrid.DataSource = usersInRole;
        UsersInRoleGrid.DataBind();
    }
}

Upvotes: 0

Views: 195

Answers (1)

Animal Style
Animal Style

Reputation: 709

You have to set the autopostback="true" on the checkbox.

<asp:CheckBox ID="adminCB" runat="server" Text="Administrator" OnCheckedChanged="Admin_CC" AutoPostBack="true" /><br />
<asp:CheckBox ID="userCB" runat="server" Text="User" OnCheckedChanged="User_CC" AutoPostBack="true" /> </td>

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.autopostback.aspx

Upvotes: 4

Related Questions