views:

275

answers:

8

Hi All,

I like to call a JavaScript function from c#. Can any one can give me code snippet.

More detail...

I have a asp.net page which has a asp button. when i click that button, i like to call javascript function.

like wise....

in my asp.net page,

<button id="save" onclick="i like to call a method resides in asp.net page'>Save</button>

More and more details... when click the asp.net button, i like to perform some server side action and then like to call a javascript function from there itself...

A: 
someFunction();

are you serious?

Andrey
I don't know anywhere in C# where you can call a Javascript method directly
masenkablast
@masenkablast - From the poster's edit, he is trying to call a JavaScript function in the browser.
mbeckish
Yea, but I downvoted before then when the question was strictly how to call JavaScript from C#... my vote can't be edited until Andrey updates his answer
masenkablast
A: 

Are you asking about how to execute a .js file on the local machine (i.e. Windows Script Host) or are you talking about doing something to the browser in an ASP.NET application?

Both of these seem a bit odd, to be honest. Be more specific in your question. Give us examples of what you've tried to do. What are you trying to achieve?

Damian Powell
+1  A: 

You can't "call" a Javascript function from ASP.NET C# code-behind. You can write additional Javascript to the webpage. By the time the page is sent back to the user and the Javascript exists, your code-behind is gone. You can write out to a Literal or do a Response.Write()

Response.Write("<script language='javascript'>alert('Hellow World');</script>");
masenkablast
I'm just guessing that he's talking about ASP.NET... it's based off of his previous questions
masenkablast
+1 for Response.Write(), which is more C#-calling-JS than ASPX pages. Kinda-Sorta.
Craig Walker
+2  A: 

On the assumption that you're coding in ASP.NET (including MVC), calling a JavaScript function would mean embedding the call in JavaScript into your ASPX code, like so:

<script type="text/javascript">
  doSomething();
</script>

You do have the opportunity to pass information from your C# to the JS call, just as you would have any other code alter the results of your ASPX:

<script type="text/javascript">
  doSomething("<%= GetSomeTextFromCSharp();  %>");
</script>

This is really stretching the definition of "calling JavaScript from C#" though. What you're doing is having your C#/ASPX code generate HTML/JavaScript, which the browser then interprets as it would any other HTML/JS (regardless of how it was generated).

Perhaps you could explain what you're trying to do a bit more.

Craig Walker
+1 He's asking to call JavaScript from C# but your method is TECHNICALLY the preferred way of doing this amongst experienced devs
masenkablast
Agreed... it's not quite the most exact answer to his question but it is the best practice.
Craig Walker
+1  A: 

Sarathi, based on your recent update, it's not clear that you need any C# interaction at all. Your <button> appears to be strictly client-side (ie: HTML) with no ASP.NET interaction within it. To call your JavaScript function you'd attach the function call to the onclick attribute of the button tag:

<button id="save" onclick="mySaveFunction();'>Save</button>

Note that mySaveFunction() just needs to be defined in the browser's load stack for the current page. That means it could be defined in any of:

  • The ASPX page that holds the <button>
  • The Master page for the current ASPX page
  • One of the User controls (or MVC partials) loaded by the current ASPX page
  • An external JavaScript file that's loaded by one of the above.

Lastly, I'd just like to reiterate that there's nothing particularly C#/ASP.NET-specific about this. You could do the same with any language/framework, including static HTML files. Your question appears to be entirely JavaScript-dependent.

Craig Walker
+2  A: 

For an asp:button you use OnClientClick

<asp:Button id="myid" runat="server" OnClientClick="alert('test')" />
Mikael Svenson
A: 

For the window object:
http://msdn.microsoft.com/en-us/library/ms536420%28VS.85%29.aspx

window.execScript

For the page pbject:
http://msdn.microsoft.com/en-us/library/dfbt9et1%28v=VS.71%29.aspx

RegisterClientScriptBlock

RegisterOnSubmitStatement

RegisterStartupScript

etc ...

Blessed Geek
Correct me if I'm wrong, but isn't window.execScript IE-only?
Craig Walker
+1 @Craig Walker This article hints that this is an IE-only method:http://ajaxian.com/archives/evaling-with-ies-windowexecscript
masenkablast
+1  A: 

Hi

i tried with this code it works for me check whether it helps

1)

Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "alert('Informations');", true); 

The other way is call the javascript method which is written in source page

Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "xyz();", true); 
GayaMani