Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
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 18-Jul-12 3:05am
Edited 18-Jul-12 20:30pm
zyck2.3K
v3
Comments
dholakiya ankit at 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 at 26-Jun-14 10:25am
   
ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "script", "<script type='text/javascript'>alert('Record deleted successfully.');</script>", false);
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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);
}
  Permalink  
Comments
paraqbaji at 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 at 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: bad
good
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
  Permalink  
Rate this: bad
good
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);
  Permalink  
Comments
Kishor Khatri at 3-May-14 1:15am
   
Thanx Suvabrata Roy
It's work for me....
Suvabrata Roy at 5-May-14 2:27am
   
Welcome...
alirezajo0n at 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 at 22-Jul-14 12:19pm
   
I did not get how why you required a JavaScript to C#
Rate this: bad
good
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);
 
}
  Permalink  
Rate this: bad
good
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>");
}
  Permalink  
Comments
Farshad72 at 22-Feb-14 4:03am
   
could you upload that library for ClientScript?
Anna SB at 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: bad
good
Please Sign up or sign in to vote.

Solution 7

in addition to above methods,you can try use client button
  Permalink  
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 325
1 DamithSL 300
2 Sergey Alexandrovich Kryukov 289
3 CPallini 235
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 22 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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