2

On a click event I call the same JavaScript function 3 times but the javascript function itself only execute once.

BLL.Common.executeJSFunction("DoSomething();");
BLL.Common.executeJSFunction("DoSomething();");
BLL.Common.executeJSFunction("DoSomething();");

The method:

public static void executeJSFunction(string jsFunction)
    {
        var page = HttpContext.Current.Handler as Page;
        ScriptManager.RegisterStartupScript(page, page.GetType(), "Exec", jsFunction, true);
    }

Java script function:

function DoSomething()
{
   alert('Hello Word');
}

ps: There is a ScriptManager and a update panel on the page just in case....

0

6 Answers 6

6

You overwrite the key with each function call, try:

BLL.Common.executeJSFunction("DoSomething();", "Exec1");
BLL.Common.executeJSFunction("DoSomething();", "Exec2");
BLL.Common.executeJSFunction("DoSomething();", "Exec3");

public static void executeJSFunction(string jsFunction, string key)
{
    var page = HttpContext.Current.Handler as Page;
    ScriptManager.RegisterStartupScript(page, page.GetType(), key, jsFunction, true);
}
Sign up to request clarification or add additional context in comments.

Comments

4

I believe this is happening because you're assigning the same key ("Exec") to the scripts. Assign a different key for each, or just do this:

BLL.Common.executeJSFunction("DoSomething();DoSomething();DoSomething();");

Comments

4

Right now you are registering the same code 3 times under the key Exec. You need to use different keys for your script registration:

public static void executeJSFunction(string key, string jsFunction)
    {
        var page = HttpContext.Current.Handler as Page;
        ScriptManager.RegisterStartupScript(page, page.GetType(), key, jsFunction, true);
    }

BLL.Common.executeJSFunction("Exec1", "DoSomething();");
BLL.Common.executeJSFunction("Exec2", "DoSomething();");
BLL.Common.executeJSFunction("Exec3", "DoSomething();");

See more here: http://msdn.microsoft.com/en-us/library/bb310408.aspx

Comments

2

Use different RegisterStartupScript method's key parameter value on each executeJSFunction method call. You may use this: ScriptManager.RegisterStartupScript(page, page.GetType(), Guid.NewGuid().ToString(), jsFunction, true);

1 Comment

thx yuri, since I tried @JamesJohnson solution first I will mark his as resolved! but thx a lot!
2

I believe your problem is being caused because you're passing the same 'key' parameter ("Exec") each time you call the RegisterStartupScript function.

See here for further info - http://forums.asp.net/t/1365260.aspx/1

Comments

0

If we need to call a javascript function more than once from c#,we must have to change the key parameter(3rd parameter),because browser will consider it as a same request and it will not process that request.Consider the below LOC

   var key="firstRequest";
    var page = HttpContext.Current.Handler as Page;
        ScriptManager.RegisterStartupScript(page, page.GetType(), key, "FunctionName()",true);

Now we have to change the key to call the same function again,

   key="secondRequest";
   var page = HttpContext.Current.Handler as Page;
        ScriptManager.RegisterStartupScript(page, page.GetType(), key, "FunctionName()",true);

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.