priyanka.sarkar priyanka.sarkar - 5 months ago 23
Javascript Question

How to call multiple jquery function from .cs file?

I have 2 JQuery functions which I want to call from PageLoad event. The current implementation is as under

ScriptManager.RegisterStartupScript(this.Page, this.GetType(),
"script", "Function1();", true);

ScriptManager.RegisterStartupScript(this.Page, this.GetType(),
"script", "Function2();", true);


The problem is that, the first function is invoked i.e. Function1 and not Function2.

If I change the order of invocation, then Function2 and not Function1 is called.

How to execute both the functions ?

Thanks in advance.

Answer

You are probably overwritting previous script block with later as you have same key script for both blocks in ScriptManager.RegisterStartupScript call. Give unique name to each script block e.g. name first block as script1 and second as script2 will allow you to call both functions.

SriptManager.RegisterStartupScript(this.Page, this.GetType(), "script1", "Function1();", true);

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script2", "Function2();", true);

In this particular scenario you do not need to register script block twice rather you can have multiple javascript stements in once RegisterStartupScript.

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", 
                                    "Function1();Function2();", true);
Comments