Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling JavaScript Function From CodeBehind

People also ask

Can we call JavaScript function from code behind C#?

You cannot. Codebehind is running on the server while JavaScript is running on the client. However, you can add <script type="text/javascript">someFunction();</script> to your output and thus cause the JS function to be called when the browser is parsing your markup.

How Call JavaScript function from VB.Net code behind?

Using RegisterStartupScript will place the call to showDisplay() at the very bottom of your form, so it will be rendered last and your javascript function will have already been rendered and available.

How do I call a JavaScript function with parameters from code behind C#?

In order to call the JavaScript function with parameter from Code Behind, one has to make use of the RegisterStartupScript method of the ClientScript class in ASP.Net using C# and VB.Net. The following HTML Markup consists of an ASP.Net Button and a Label control.


You may try this :

Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","MyFunction()",true);

C# to JavaScript: you can register script block to run on page like following:

ClientScript.RegisterStartupScript(GetType(),"hwa","alert('Hello World');",true);

replace alert() part with your function name.

For calling C# method from JavaScript you can use ScriptManager or jQuery. I personally use jQuery. You need to decorate the method that you want to call from JavaScript with WebMethod attribute. For more information regarding calling C# method (called PageMethod) from jQuery you can refer to Dave Ward's post.


Calling a JavaScript function from code behind

Step 1 Add your Javascript code

<script type="text/javascript" language="javascript">
    function Func() {
        alert("hello!")
    }
</script>

Step 2 Add 1 Script Manager in your webForm and Add 1 button too

Step 3 Add this code in your button click event

ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "Func()", true);

If you need to send a value as a parameter.

string jsFunc = "myFunc(" + MyBackValue + ")";
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "myJsFn", jsFunc, true);

You can not do this directly. In standard WebForms JavaScript is interpreted by browser and C# by server. What you can do to call a method from server using JavaScript is.

  • Use WebMethod as attribute in target methods.
  • Add ScriptManager setting EnablePageMethods as true.
  • Add JavaScript code to call the methods through the object PageMethods.

Like this:

Step 1

public partial class Products : System.Web.UI.Page 
{ 
    [System.Web.Services.WebMethod()] 
    [System.Web.Script.Services.ScriptMethod()] 
    public static List<Product> GetProducts(int cateogryID) 
    {
        // Put your logic here to get the Product list 
    }

Step 2: Adding a ScriptManager on the Page

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />

Step 3: Calling the method using JavaScript

function GetProductsByCategoryID(categoryID)
{
    PageMethods.GetProducts(categoryID, OnGetProductsComplete);
}

Take a look at this link.

To call a JavaScript function from server you can use RegisterStartupScript:

ClientScript.RegisterStartupScript(GetType(),"id","callMyJSFunction()",true);