0

I have the following in C#:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <span id="header" class="hideMe">
            (other irrelevant tags and controls)
        </span>
        <div>
            (other irrelevant tags and controls)
        </div>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnSubmit" />
    </Triggers>
</asp:UpdatePanel>

And the following jQuery

if ($) {
  $(document).ready(function () {

    $('#header').click(function ($e) {
      if ($(this).hasClass('hideMe')) {
        $('#header').removeClass('hideMe');
        $('#header').addClass('showMe');
      }
      else {
        $('#header').removeClass('showMe');
        $('#header').addClass('hideMe');
      }
    }

  });
}

The jQuery works just fine (ie - if I click upon the header span and the hideMe class is current then it'll remove hideMe and add showMe, and vice-versa).

However, when I click upon the btnSubmit then an asynchronous postback occurs and causes the header span to reset to its original state (which would be with the hideMe class applied).

How do I go about getting the header span to remain in the state that it was when the submit button is clicked and to not revert back to the state that it was at page load?

Jagd
  • 7,169
  • 22
  • 74
  • 107

2 Answers2

5

What has worked for me:

$(document).ready(function() { 
    // bind your jQuery events here initially 
}); 

var prm = Sys.WebForms.PageRequestManager.getInstance(); 

prm.add_endRequest(function() { 
    // re-bind your jQuery events here 
}); 

Taken from: jQuery $(document).ready and UpdatePanels?

Community
  • 1
  • 1
Gerson Beltrán
  • 402
  • 4
  • 8
  • Does this need to be inside of a Script tag or a script reference tag or it doesn't matter? I.e. does it matter whether the script that has this is being referenced like – Alexander Ryan Baggett Aug 29 '13 at 19:01
0

Try using some event delegation.

if ($) {
  $(document).ready(function () {

    $('body').delegate('#header','click',function ($e) {
      if ($(this).hasClass('hideMe')) {
        $(this).removeClass('hideMe');
        $(this).addClass('showMe');
      }
      else {
        $(this).removeClass('showMe');
        $(this).addClass('hideMe');
      }
    }

  });
}

Edit: As noted in comments, this will not maintain state. For simplicity, I would suggest using a global variable for that purpose, however you would need to run some kind of callback on the async call to apply the classes as needed based on previous state.

Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • 1
    Only going to help halfway. The event binding will be saved (great!) but the #header being returned will not necessarily have the right class. Could store in a variable, get the state right before the POST and re-apply in the success callback, POST to server and have the server send back with correct class already rendered, etc.. – Greg Pettit Apr 02 '12 at 19:16