Click here to Skip to main content
14,388,907 members
Rate this:
Please Sign up or sign in to vote.
how to call javascript function in c#

c# code behind:
protected void print_click(object sender,eventargs e)
{
  //When i clicked this button i need to call javascript function
}


javascript code

function example()
{
var prtContent = document.getElementById('<%=GridView3.ClientID %>');
//in between these two lines many lines there its not necessary here
window.print();
}
Posted
Updated 27-Aug-19 20:18pm
v3
Comments
Dholakiya Ankit 9-Aug-13 0:41am
   
try this one

ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "script", "<script type='text/javascript'>alert('Record deleted successfully.');</script>", false);
Member 10851921 26-Jun-14 10:25am
   
ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "script", "alert('Record deleted successfully.');", false);
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi,

You can execute javascript file using,

ScriptManager.RegisterStartupScript[^]

Example: How to execute javascript in C#[^]

There are other way as well. Let us know if it is not feasible for you,

Thanks
-Amit.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Write function this way
protected void print_click(object sender,eventargs e)
{
  //when i click this button i need to call javascript function
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(@"<script language='javascript'>");
            sb.Append(@"example();");
            sb.Append(@"</script>");
     System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "JCall1", sb.ToString(), false);
}
   
Comments
paraqbaji 15-Apr-14 7:48am
   
@NothingToLoose,

Compiler gives error to Line: sb.Append(@"<script language="'javascript'">");

Do you have to say anything about it?

Error: Too many characters in Character Literal.
Anna SB 14-Jun-14 0:36am
   
Problem on line 4: Too many characters in character literal.
Resolve for the problem: convert string quotes from C-Based to HTML-Based ones to avoid C# from reading them as a String Close Tag.
Will be: sb.Append(@"<script language='javascript'>");
Rate this:
Please Sign up or sign in to vote.

Solution 8

Read all of the existing answers, but first, understand one fundamental things: you cannot even talk about "call".

C# is on the server side, Javascript — on the client side. It could be anything but "call". What is it? First of all, you need to understand some basics of the Web functionality. All server-side code does is receiving HTTP request from client side, process it, and then generate HTTP response and send it to client side. You can generate any thinkable content. And part of the content can be Javascript. You just generate Javascript, possibly in a parametrized way, using some data. How you do it? This is a separate question. You can generate HTTP response directly, or use RegisterStartupScript, or anything. This is the essence of how it works.

—SA
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,

Try this

string str="<script>alert(\"ok\");</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", str, false);
   
Comments
Kishor Khatri 3-May-14 1:15am
   
Thanx Suvabrata Roy
It's work for me....
Suvabrata Roy 5-May-14 2:27am
   
Welcome...
alirezajo0n 22-May-14 4:03am
   
how to convert this code to csharp?
<script type="text/javascript">
function Clear(mySearch) {
document.getElementById("mySearch").value = "";

}
</script>
Suvabrata Roy 22-Jul-14 12:19pm
   
I did not get how why you required a JavaScript to C#
Rate this:
Please Sign up or sign in to vote.

Solution 4

here is two method. here you have not cleared that 1. after executing java function do you want to some in print_click event in cs. or 2. In print_click event excecute than you want js function

in 1. condition you can write simply
print.Attributes.Add("onclick", "example();");


in 2. condition you can write

protected void print_click(object sender,eventargs e)
{
  //when i click this button i need to call javascript function

ScriptManager.RegisterStartupScript(Page, GetType(), "JsStatus", "example();", true);

}
   
Rate this:
Please Sign up or sign in to vote.

Solution 6

Hi,

Use this code which i have used and it's working

protected void print_click(object sender,eventargs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "print", "<script>example();</script>");
}
   
Comments
Farshad72 22-Feb-14 4:03am
   
could you upload that library for ClientScript?
Anna SB 14-Jun-14 0:43am
   
Library?
It's a property inherited from System.Web.UI.Page class into the page you are using.
ClientScriptManager System.Web.UI.Page.ClientScript;
Rate this:
Please Sign up or sign in to vote.

Solution 9

All solutions you added are excellent in syntax-level. But you should remember, there's a delay in runtime-level between sever-side and client-side scripts when you're trying to run the script within the PageLoad. So you should give enough time to the ASP.net server side to regenerate the webpage, as it should be ...

ClientScript.RegisterStartupScript(this.GetType(),
                                   "script",
                                   "setTimeout(YourFunction1, 1000);",
                                   true);


I think 1s is enough for regeneration of a little form... increase it depending on your need.
   
Rate this:
Please Sign up or sign in to vote.

Solution 12

found a nice write up on how to call it . javascript - parse and execute JS by C# - Stack Overflow[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100