简体   繁体   中英

How can I use jquery to display updates when I detect them in my code behind file?

I have an asp.net application and in the UI I have a jquery plugin called growel. When I detect that someone have changed their account (code behind) I need to call the jquery plugin to display but this is on the client side.

But how is this possible, I cant call jquery from code behind code (C#), has anyone a workaround or am i missing something.

If you know about it in the code-behind, presumably it's before the page is rendered. A simple solution would be to write the message you want to display in growl into a javascript variable (inside <script type="text/javascript"> ) inside the page, check for that variable during inside $(document).ready() , and if it's there, then display it in the growl.

The client side script would have to ping the server for updates and, if there, display it. You could do that with a timer and an ajax call.

If you can create a page method that exposes the progress, you can do this via a polling ajax call, something like this:

ASP.Net page side:

[WebMethod]
public static string GetProgress()
{
  //Put progress logic here, whatever you need to return/display
  return "Still processing";
  //When finished: return "All Done!";
}

jQuery side:

function updateProgress() {
  $.ajax({
    type: "POST",
    url: "MyPage.aspx/GetProgress",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      $("#statusMsg").text(msg); //Display the message
      if (msg !== "All Done!") //Stop looping when your "completed" comes back
        setTimeout(updateProgress, 1000); //Update status again in 1 second
    }
  });
}

In whatever method you are kicking things off, just call updateProgress() at the end. This is a very lightweight way of doing things, you're making a simple http request sending very little data, not viewstate and all that, and getting back only the response you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM