1

I'm trying to execute JQuery after an ASP.Net Microsoft AJAX post back.

When a user clicks on a link, Microsoft AJAX is used to update some fields in the DB and if success a label appears informing the user the change has been made. Unfortunately the label is not very obvious and I would like to use to fade the background from red to white.

The problem is that when visible=false is set on the label, the resulting html does not include the label(span). Does anyone know how to execute JQuery after an ASP.Net Microsoft AJAX post back, or another solution to achieve the same affect?

1
  • @Alison yes I'm using UpdatePanels, I also have one than one Microsoft AJAX link/method on each of the panels Commented Jun 6, 2011 at 16:25

4 Answers 4

2

This is how you can execute a random javascript after an ASP.NET Ajax postback

function executeThis(){
//code here to fade in out the label that comes


var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.remove_pageLoaded(executeThis); //job done, remove this so that it is not fired again.
}


$("link").click(function(){
                var prm = Sys.WebForms.PageRequestManager.getInstance();
                prm.add_pageLoaded(executeThis); //this will register executeThis function with MS Ajax libraries which will fire it after next posback
               //the post back happens here.
  });
Sign up to request clarification or add additional context in comments.

6 Comments

@Nikhil how would I know which method had been fired?
The method named "executeThis" will be fired. Basically teh idea is to register a callback function (called whatever you like) with MS Ajax scripts and have the scripts fire that function on the next postback once the page has been loaded.
@Nikhil Sorry I re-read my comment and it very clear. What I meant was how do I know which MS Ajax method was called. I only want the JQuery to execute if a particular link is clicked on. Or should I wrap the add_pageloaded in $('linkName').clicked
Actually the other way round. I am updating the answer to (hopefully!) solve this problem
@Nikhil that works, except if the link is clicked on a second time the function isn't called.
|
2

You could try this in the postback

ScriptManager.RegisterStartupScript(Me.Form, Me.GetType(), "FunctionName", "FunctionName();", True)

This will call the javascript function FunctionName() sfter the postback is complete

Comments

2

Sort of the same as what @Nikhil has said, something very similiar and what I always use:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(functionName)

Where functionName is the name of the function containing whatever you want called. This ensures that the function is called whenever the page/panel is reloaded/refreshed.

Comments

0

Here is a bit of pseudocode to get you going:

  1. If no success, keep the asp:Label visible property = true but give it a CSS class with display set to none
  2. On page load, execute the following:

    ScriptManager.RegisterStartupScript(Me.Form, Me.GetType(), "ShowError", "ShowError();", True);

  3. In your Javascript, add the following:

    function ShowError() { $('#myLabelID').show().fadeOut(); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.