Click here to Skip to main content
12,950,090 members (44,420 online)
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 18-Jul-12 2:05am
Updated 18-Jul-12 19:30pm
zyck2.5K
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: 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 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: 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 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: 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 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: 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
Top Experts
Last 24hrsThis month
OriginalGriff 5,749
CHill60 3,380
Maciej Los 2,953
Jochen Arndt 1,935
ppolymorphe 1,820


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 25 Feb 2016
Copyright © CodeProject, 1999-2017
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