OnCheckedChanged event not firing when Checkboxes within Repeater are checked

19,326

Solution 1

The Autopostback on the checkboxes is causing the SQLDatasource to just revert to the original Stored Procedure, as it hits page load and just ignores the oncheckchanged events.

What I did is I took all of my databinding events in pageload and put them inside of a if (!IsPostBack) clause, so that when the Autopostback hits, it doesn't rebind the SQLDS to the original value.

This way, when the autopostback occurs, there's nothing for it to do, and it hits the OnCheckedChanged events as it's supposed to.

Thank you everyone for reading and responding.

Solution 2

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.

Solution 3

ASP.NET Repeater has an event that causes a round trip from client to occur. The event is called ItemCommand. Check out this link :- http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemcommand.aspx

Hope this helps

Share:
19,326

Related videos on Youtube

RockiesMagicNumber
Author by

RockiesMagicNumber

ASP.NET/C# Software Engineer. Skilled in MS SQL Server and can work in a Microsoft Access VBA environment. Still learning, still asking questions.

Updated on June 04, 2022

Comments

  • RockiesMagicNumber
    RockiesMagicNumber almost 2 years

    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.

  • RockiesMagicNumber
    RockiesMagicNumber over 12 years
    I saw that link as well, and tried implementing it. It doesn't solve the "re-check an unchecked link" problem.
  • RockiesMagicNumber
    RockiesMagicNumber over 12 years
    I did just that, set AutoPostBack=True as well as set up Triggers on the UpdatePanel in the form of <asp:AsyncPostBackTrigger ControlID="Checkbox1" EventName="CheckedChanged" /> and <asp:AsyncPostBackTrigger ControlID="Checkbox2" EventName="CheckedChanged" /> - The issue remains with the checkboxes not hitting their checkedchanged events.
  • RockiesMagicNumber
    RockiesMagicNumber over 12 years
    This looks like it would be useful, but I'm not sure how to implement it. The OnCheckedChanged events need their own methods, so I'm not sure what to put inside a Repeater1_ItemCommand method.
  • James Johnson
    James Johnson over 12 years
    @RockiesMagicNumber: Please see my answer. I think this will solve your problem.