How to call javascript function from c#

前端 未结 7 1535
盖世英雄少女心
盖世英雄少女心 2020-12-11 19:18

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

相关标签:
7条回答
  • 2020-12-11 20:05

    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>");
    
    0 讨论(0)
  • 2020-12-11 20:08

    For an asp:button you use OnClientClick

    <asp:Button id="myid" runat="server" OnClientClick="alert('test')" />
    
    0 讨论(0)
  • 2020-12-11 20:08

    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.

    0 讨论(0)
  • 2020-12-11 20:10

    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); 
    
    0 讨论(0)
  • 2020-12-11 20:12

    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.

    0 讨论(0)
  • 2020-12-11 20:13

    you can call javascript function from code behind page ..for example you have closewindow function definition part in javasript..if you want to execute that function,you can write following codings in any click event in code behind page..

    ClientScript.RegisterClientScriptBlock(GetType(), "close", "<script language=javascript>Closewindow();</script>", false);
    
    0 讨论(0)
提交回复
热议问题