Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How To Call C# Function From JavaScript

How To Call C# Function From JavaScript

Scheduled Pinned Locked Moved ASP.NET
questioncsharpjavascripttutorial
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrgaddam
    wrote on last edited by
    #1

    Hi, This is from Chandrakanth. I would like to call a function which is in C#, From JavaScript. How can i go for that? Can any one give me repply for this. Thanks And Regards chandrakanth

    Chandrakanth

    R S Y 3 Replies Last reply
    0
    • M mrgaddam

      Hi, This is from Chandrakanth. I would like to call a function which is in C#, From JavaScript. How can i go for that? Can any one give me repply for this. Thanks And Regards chandrakanth

      Chandrakanth

      R Offline
      R Offline
      RichardGrimmer
      wrote on last edited by
      #2

      Unfortunately, this isn't very straightforward - js runs in the browser on the client, the C# code on the server. To call anything on the server you'd need to use AJAX to make a call back to the server. I tend to do this with a callbacks page - straight apsx page, no markup, and a big ol switch statement in page load, which looks for a querystring param which indicates the method in the callbacks file I wanna call...

      C# has already designed away most of the tedium of C++.

      Y 1 Reply Last reply
      0
      • R RichardGrimmer

        Unfortunately, this isn't very straightforward - js runs in the browser on the client, the C# code on the server. To call anything on the server you'd need to use AJAX to make a call back to the server. I tend to do this with a callbacks page - straight apsx page, no markup, and a big ol switch statement in page load, which looks for a querystring param which indicates the method in the callbacks file I wanna call...

        C# has already designed away most of the tedium of C++.

        Y Offline
        Y Offline
        YeHtut
        wrote on last edited by
        #3

        document.getElementbyID("IDName").Click() or FireEvent

        M 1 Reply Last reply
        0
        • Y YeHtut

          document.getElementbyID("IDName").Click() or FireEvent

          M Offline
          M Offline
          Michael Sync
          wrote on last edited by
          #4

          No, Ko Ye Htut. The question that he was asking is about calling the server-side function from client-side. It's true that document.getElementbyID("IDName").Click() can invote the server-side click event. but your code won't work if he wants to call the normal server-side function (e.g. public void MyMethod(){ })

          YeHtut wrote:

          FireEvent

          You can't just fire the event to invote the server-side method from client-side script.. but it is possible to do with Ajax.

          Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

          1 Reply Last reply
          0
          • M mrgaddam

            Hi, This is from Chandrakanth. I would like to call a function which is in C#, From JavaScript. How can i go for that? Can any one give me repply for this. Thanks And Regards chandrakanth

            Chandrakanth

            S Offline
            S Offline
            Sun Rays
            wrote on last edited by
            #5

            Hi, you can try this. Make Javascript function like this. function GetClientScript() { var str= "My test"; var test = '<%= GetFunction()%>'; alert(test); } Here GetFunction() is C# function. This function must return something else this cant work. Now call this function onClientClick of any asp.net linkbutton or button. And here you are..

            Thanks, Sun Rays To get something you must have to try once. My Articles

            1 Reply Last reply
            0
            • M mrgaddam

              Hi, This is from Chandrakanth. I would like to call a function which is in C#, From JavaScript. How can i go for that? Can any one give me repply for this. Thanks And Regards chandrakanth

              Chandrakanth

              Y Offline
              Y Offline
              YeHtut
              wrote on last edited by
              #6

              U try to do like this. This is written in code behind file. public void ServerSideFunction() { ... } This javascript is I was written in Aspx file. <script> <%ServerSideFunction;%> </script> I already test and work it.

              Y M 2 Replies Last reply
              0
              • Y YeHtut

                U try to do like this. This is written in code behind file. public void ServerSideFunction() { ... } This javascript is I was written in Aspx file. <script> <%ServerSideFunction;%> </script> I already test and work it.

                Y Offline
                Y Offline
                YeHtut
                wrote on last edited by
                #7

                U try to do like this. This is written in code behind file. public void ServerSideFunction() { ... } This javascript is I was written in Aspx file. <script> <%ServerSideFunction();%> </script> I already test and work it.

                1 Reply Last reply
                0
                • Y YeHtut

                  U try to do like this. This is written in code behind file. public void ServerSideFunction() { ... } This javascript is I was written in Aspx file. <script> <%ServerSideFunction;%> </script> I already test and work it.

                  M Offline
                  M Offline
                  Michael Sync
                  wrote on last edited by
                  #8

                  Seem like you are confusing. The server-side script can be written in both class file (e.g. .cs or .vb) and aspx file. But if we want to write the server-side script in aspx file, you have to use <% %>. So, the way that you showed is just writing the serverside code in aspx file. And it doesn't mean that you are calling the serverside function from client-side ( Javascript or vbscript or etc). still confused? Okay. Let's say.. I want to call this function ( ServerSideFunction ) from Javascript when I click the button ( input button. not server-control). So, ServerSideFunction() should be invoked only when I click the button. (not on pageload). How to do that? :)

                  Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

                  Y 1 Reply Last reply
                  0
                  • M Michael Sync

                    Seem like you are confusing. The server-side script can be written in both class file (e.g. .cs or .vb) and aspx file. But if we want to write the server-side script in aspx file, you have to use <% %>. So, the way that you showed is just writing the serverside code in aspx file. And it doesn't mean that you are calling the serverside function from client-side ( Javascript or vbscript or etc). still confused? Okay. Let's say.. I want to call this function ( ServerSideFunction ) from Javascript when I click the button ( input button. not server-control). So, ServerSideFunction() should be invoked only when I click the button. (not on pageload). How to do that? :)

                    Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

                    Y Offline
                    Y Offline
                    YeHtut
                    wrote on last edited by
                    #9

                    I think so, anyway should be postback and call the Server Side function. Otherwise can not be called the server side function. AJAX is called partial postback and call the server function. So Main page is never refresh. I know like this. If you know that ow to do with other way, please share it to me. Thanks for best suggest. :)

                    M 1 Reply Last reply
                    0
                    • Y YeHtut

                      I think so, anyway should be postback and call the Server Side function. Otherwise can not be called the server side function. AJAX is called partial postback and call the server function. So Main page is never refresh. I know like this. If you know that ow to do with other way, please share it to me. Thanks for best suggest. :)

                      M Offline
                      M Offline
                      Michael Sync
                      wrote on last edited by
                      #10

                      As I said in this post[^], Ajax is the only solution for this problem. I was pointing out the mistake that you wrote in this post[^]. Thank you. :)

                      Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups