Home 
 
 
 
 
 Blog 
 
 
 
Sytling Image

Posts Tagged ‘oncheckchanged’

Stacking PostBack Events

February 11th, 2011 - Be the First Comment!

The other day at work I was working through a problem with a coworker. The short of it is that he had something going on that I didn’t even know could happen. I made a small project to experiment with this and it attached for download if you want to try it out. It is in ASP.NET and C#. He was stacking postbacks (accidently, causing strange behavior). He had a checkbox with an OnCheckChanged event specified, but it also had AutoPostBack=”false” on it.

Check the checkbox first, then click the Submit button.

    /* Front End Code
     * One check box has a normal postback the other creates
     * a stacked PostBack.
     */
    <div>
        <asp:CheckBox ID="cb1" Text="CheckBox 1 (AutoPostBack=false)" OnCheckedChanged="cb1_CheckChanged" AutoPostBack="false" runat="server" />&nbsp;
        <asp:CheckBox ID="cb2" Text="CheckBox 2" OnCheckedChanged="cb2_CheckChanged" AutoPostBack="true" runat="server" />&nbsp;
        <br />
        <br />
        <asp:Label ID="lblTarget" runat="server" />
        <br />
        <br />
        <asp:Button ID="Submit" Text="Submit" runat="server" OnClick="Submit_Click" />&nbsp;
        <asp:Button ID="Reset" Text="Reset" OnClick="Reset_Click" runat="server" />
    </div>

    /* Back End Code
     * The back end code just adds the name of the ID of the button lblTarget
     */
        protected void cb1_CheckChanged(object sender, EventArgs e)
        {
            if (lblTarget.Text.Equals(lblTargetText))
            {
                lblTarget.Text = " cb1 ";
            }
            else
            {
                lblTarget.Text += " cb1 ";
            }
        }

Effectively he was doing something that requires a PostBack then saying not to PostBack. This leads to a stacking effect (which was unknown to us until recently). On the next PostBack  the method for the OnCheckedChanged event was fired then the actual item that caused the PostBack’s method is executed. I never realized you could queue up PostBack events. Hopefully this will help someone else out.

PostBack Event Experiments

Styling Image