5

I use in my web part a Session[""] and would like to notify the user when the session is expired. I check at the PostBack event if the session is already okay, if not, I will execute a JS function which shows the SP.UI.Status warning message.

Now I don’t know how I could execute the JS. I tested it the ScriptManager, but then I get a error, that the SP.UI.Status is not define. How could I add the SP.UI.Status at the PostBack?

[Edit]

The same issue occurs with SP.UI.Notify and is fixed with the same solution.

10

My guess for the reason you are having trouble with calling SP.UI.Status.addStatus is because the SP.js script file (which contains the namespace and relevant functions) is loaded on demand.

In order to call SP.UI.Status.addStatus() as soon as the page loads, you'll need to make sure you use the script on demand framework for doing so.

Something like this

ExecuteOrDelayUntilScriptLoaded(mysessionexpiredfunction, 'sp.js')

  • 1
    That exactly what I mean! I didn’t know anything about the ExecuteOrDelayUntilScriptLoaded() method. I could solved it with: this.Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "ExecuteOrDelayUntilScriptLoaded(test, 'sp.js');", true);. Thanks!!! – user5167 Nov 22 '11 at 13:51
0

The easiest solution would be to check if the session has expired client side onclick(). Then it's merely a function call to the SP.UI.Status.addStatus()

If that's not possible, there are some alternatives I've used with various success in the past. You could e.g. try to use Response.Write("<script language='javascript'>SP.UI.Status.addStatus();</script>"); like shown here.

  • Mhm how should that work? Output is not a method. I tried it with ScriptManager.RegisterStartupScript(Page, Page.GetType(), "test", "test();", true); but here i get following error: Microsoft JScript runtime error: Unable to get value of the property 'addStatus': object is null or undefined. I think at that point, the SP.UI.Status.addStatus is not ready, right? If I executed the function with a test button everything works fine. – user5167 Nov 22 '11 at 13:24
  • Yeah, you're right. If you have to do it serverside, Paul Lucas' method is better. – tarjeieo Nov 22 '11 at 14:00
0

When function with parameters view:

Pass parameters in ExecuteOrDelayuntilScriptLoaded in Javascript client Object model

You can use an function as a delegate and then call your function with in.

ExecuteOrDelayUntilScriptLoaded(function () { alertThis("Hello World") }, "core.js");

function alertThis(value)
{
    alert(value);
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy