ShadowG
ShadowG

Reputation: 683

c# Multiple calls to a javascript function not working

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....

Upvotes: 2

Views: 2136

Answers (6)

Deepak Kothari
Deepak Kothari

Reputation: 1753

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);

Upvotes: 0

Jakub Konecki
Jakub Konecki

Reputation: 46008

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

Upvotes: 4

ipr101
ipr101

Reputation: 24226

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

Upvotes: 2

James Johnson
James Johnson

Reputation: 46047

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();");

Upvotes: 4

IUnknown
IUnknown

Reputation: 22448

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);

Upvotes: 2

Joe
Joe

Reputation: 82554

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);
}

Upvotes: 6

Related Questions