Click here to Skip to main content
15,884,976 members
Please Sign up or sign in to vote.
2.67/5 (4 votes)
how to call javascript function in c#

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


javascript code

JavaScript
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 19: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);

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.
 
Share this answer
 
Write function this way
XML
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);
}
 
Share this answer
 
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'>");
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
 
Share this answer
 
Hi,

Try this

C#
string str="<script>alert(\"ok\");</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", str, false);
 
Share this answer
 
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#
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
C#
print.Attributes.Add("onclick", "example();");


in 2. condition you can write

C#
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);

}
 
Share this answer
 
Comments
Marlon Cano 27-Oct-20 11:06am    
ty
Hi,

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

C#
protected void print_click(object sender,eventargs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "print", "<script>example();</script>");
}
 
Share this answer
 
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;
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 ...

C#
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900