views:

1360

answers:

9

As the title suggests, I am having trouble maintaining my code on postback. I have a bunch of jQuery code in the Head section and this works fine until a postback occurs after which it ceases to function!

How can I fix this? Does the head not get read on postback, and is there a way in which I can force this to happen?

JavaScript is:

        <script type="text/javascript">
        $(document).ready(function()
        {
            $('.tablesorter tbody tr').tablesorter();

        $('.tablesearch tbody tr').quicksearch({
            position: 'before',
            attached: 'table.tablesearch',
            stripeRowClass: ['odd', 'even'],
            labelText: 'Search:',
            delay: 100
        });
       }); 
        </script>

Thanks in advance :)

+4  A: 

If you just have that code hard coded into your page's head then a post back won't affect it. I would check the following by debugging (FireBug in FireFox is a good debugger):

  • Verify the script is still in the head on postback.
  • verify that the css classes are in fact attached to some element in the page.
  • verify that the jquery code is executing after the browser is done loading on post back.

EDIT: Are you using UpdatePanels for your post back? In other words is this an asynchronous postback or a normal full page refresh?

EDIT EDIT: AHhhhh... Ok. So if you're using UpdatePanels then the document's ready state is already in the ready so that portion of jquery code won't be fired again. I would extract the jquery delegate out to a separate function that you can also call after the async postback.

Justin Bozonier
A: 

Cheers, It's what I thought as well but:

  • The script is still in the head on postback
  • The css classes are attached, it does work when the page initally loads. Just not after a postback
  • Well, the jQuery code is not working after postback..Not sure if it's attempting to or if the whole thing is skipped?

Thanks tho :)

Damien
+1  A: 

I'm guessing that the postback pre-empts the page's onLoad event, which jQuery needs to hook into to use it's .ready().

Joel Coehoorn
A: 

Update Panels.

Damien
+1  A: 
  1. Does the script exist in the HTML code after the postback?
  2. If so, does the code get executed? Test by commenting out your code and temporarily add alert('test');
  3. If so, are the elements referenced by the code available on the page after postback?
Kon
A: 

Ok, It does exist in the HTML code after postback but it does not execute the alert after postback. It does on page load.

So basically it seems to be skipping over the head code? Is there anyway I can tell it not to do that.

Damien
You're using $(document).ready and that does not fire after an asynchronous postback. You need to tie into the UpdatePanel's events and run that code explicitly after a post back.
Justin Bozonier
So I am doing to have to use ClientScriptManager in code-behind?
Damien
A: 

Cheers Everyone :D Answered

Damien
What was the answer Damien?
Justin Bozonier
A: 

Instead of using $(document).ready you should put your code in a function called pageLoad(). The pageLoad() function is by convention wired up to be called whenever the page has a postback/asyncpostback.

A: 

put your code in

function pageLoad(sender, args) {

    /* code here */

}

instead of in $(document).ready(function() { ... });

pageLoad() is a function that will execute after all postbacks, synchronous and asynchronous. See this answer for more details

Russ Cam