2

How to call JavaScript function from ASP.NET code behind?

I have JavaScript function and I am using it in a listview with a parameter

I want to use it with another parameter in code behind ?

 <a href="ChatBox.aspx?id=<%# Eval("USR_UserID")%> " 
     onclick="openWindow(this.href,'chat<%# Eval("USR_UserID")%>',600,600);
     this.blur();
     return false;"><%# Eval("USR_UserName") %></a>

that is the listview side. How can I use openwindow function in code behind fore specific ID?

1
  • please check my answer Commented Oct 4, 2015 at 8:54

2 Answers 2

9

I would think using RegisterClientScriptBlock would be the best solution.

Lets say you have a javascript function, MyJSFunction():

function MyJSFunction() {
        ...
}

In your event, such as a Button Click, you would inject the following code:

C#:

ScriptManager.RegisterClientScriptBlock(this, typeof(System.Web.UI.Page), "MyJSFunction", "MyJSFunction();", true);

VB.NET:

ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "MyJSFunction", "MyJSFunction();", True)

This effectively inserts a call to your JavaScript from the code-behind.

Sign up to request clarification or add additional context in comments.

Comments

5

Use RegisterStartupScript or RegisterClientScriptBlock

Injecting Client-Side Script from an ASP.NET Server Control http://msdn.microsoft.com/en-us/library/aa478975.aspx

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.