Reputation: 683
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
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
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
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
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
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
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