C# – OnCheckedChanged event not firing when Checkboxes within Repeater are checked

asp.netcrepeater

I am a relatively new developer, and have only been doing this for about 6 months full time, so thank you in advance for reading and/or responding to my question

I have a databound repeater. Inside this repeater, I have a gridview, SQLDS, and 2 checkboxes. Both checkboxes have a OnCheckedChanged event and AutoPostback is set to true. The repeater has an OnItemDataBound event as well.

Here is a sample of how my code is laid out:

    <asp:Repeater ID="Repeater1" OnItemDataBound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <asp:Panel>
                <asp:UpdatePanel>
                    <ContentTemplate>
                        <asp:Checkbox ID="Checkbox1" Autopostback="True" OnCheckedChanged="CheckBox1_CheckedChanged">
                        <asp:Checkbox ID="Checkbox2"Autopostback="True" OnCheckedChanged="CheckBox2_CheckedChanged">
                        <asp:Gridview ID="Gridview1" DataSourceID="SqlDataSource1">
                        <asp:SQLDataSource ID="SQLDataSource1" SelectCommand="SP1" SelectCommandType="StoredProcedure">
                    </ContentTemplate>
                </asp:UpdatePanel>
            </asp:Panel>
        </ItemTemplate>
    </asp:Repeater>

And the C#

    protected void Checkbox1_CheckedChanged(object sender, EventArgs e)
    {
        if (Checkbox1.Checked == true)
            {
                if (Checkbox2.Checked == true)
                    SqlDataSource1.SelectCommand = "SP1";
                else
                    SqlDataSource1.SelectCommand = "SP2";
            }
            else
                SqlDataSource1.SelectCommand = "SP3";
    }
    protected void Checkbox2_CheckedChanged(object sender, EventArgs e)
    {
        if (Checkbox2.Checked == true)
            {
                if (Checkbox1.Checked == true)
                    SqlDataSource1.SelectCommand = "SP3";
                else
                    SqlDataSource1.SelectCommand = "SP2";
            }
            else
                SqlDataSource1.SelectCommand = "SP1";
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //Uses FindControl to Databind the GV and hides it if GV.Rows.Count==0
    }

I'm doing all of this within an AJAX TabPanel. I have another page where this code works perfectly, but it's not inside a repeater on that other page.

Essentially, I have a page load with a gridview, and the two checkboxes change what SP fills the gridview. The problem I'm having is that when you uncheck the checkbox (they start checked), it 1. Just rechecks itself and 2. Doesn't hit the CheckedChanged event.

Any help would be GREATLY appreciated.

Best Answer

You need to set AutoPostBack=True attribute for the checkboxes and also register/assign event handlers of all controls which are added into the UpdatePanel through Triggers property of UpdatePanel control.

Related Topic