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. Call a server function from javascript

Call a server function from javascript

Scheduled Pinned Locked Moved ASP.NET
questioncsharpjavascriptsysadmintools
9 Posts 4 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.
  • A Offline
    A Offline
    anderslundsgard
    wrote on last edited by
    #1

    How can I force a postback and call a codebehind function from javascript? I want to do something like this: aspx-file

    <script type="text/javascript">
    function OnRowSelecting(sender, args)
    {
    ServerFunc();
    }
    </script>

    Codebehind c# file

    private void ServerFunc()
    {
    //…
    }

    _____________________________ ...and justice for all

    T S C 3 Replies Last reply
    0
    • A anderslundsgard

      How can I force a postback and call a codebehind function from javascript? I want to do something like this: aspx-file

      <script type="text/javascript">
      function OnRowSelecting(sender, args)
      {
      ServerFunc();
      }
      </script>

      Codebehind c# file

      private void ServerFunc()
      {
      //…
      }

      _____________________________ ...and justice for all

      T Offline
      T Offline
      ToddHileHoffer
      wrote on last edited by
      #2

      How I usually do this which may not be the best method. Drag a button on your form. Double Click it to get your server side code method. In the pageload hide the button from the user by adding the following: this.Button1.Style.Add("display", "none"); Put your code server side in the button click's event. Then Add the following javascript to your page. function CallServerMethod() { var Button1 = document.getElementById("<%=Button1.ClientID %>"); Button1.click(); } Now you can use the javascript "CallServerMethod()"

      I didn't get any requirements for the signature

      C A 2 Replies Last reply
      0
      • A anderslundsgard

        How can I force a postback and call a codebehind function from javascript? I want to do something like this: aspx-file

        <script type="text/javascript">
        function OnRowSelecting(sender, args)
        {
        ServerFunc();
        }
        </script>

        Codebehind c# file

        private void ServerFunc()
        {
        //…
        }

        _____________________________ ...and justice for all

        S Offline
        S Offline
        Sundar_R
        wrote on last edited by
        #3

        document.forms[0].submit() isn't this a simplier way ?

        C 1 Reply Last reply
        0
        • A anderslundsgard

          How can I force a postback and call a codebehind function from javascript? I want to do something like this: aspx-file

          <script type="text/javascript">
          function OnRowSelecting(sender, args)
          {
          ServerFunc();
          }
          </script>

          Codebehind c# file

          private void ServerFunc()
          {
          //…
          }

          _____________________________ ...and justice for all

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          You realise that there's no way to do this without making an AJAX call ( which requires it's own set of work ) or refreshing the entire page ?

          Christian Graus Driven to the arms of OSX by Vista.

          1 Reply Last reply
          0
          • T ToddHileHoffer

            How I usually do this which may not be the best method. Drag a button on your form. Double Click it to get your server side code method. In the pageload hide the button from the user by adding the following: this.Button1.Style.Add("display", "none"); Put your code server side in the button click's event. Then Add the following javascript to your page. function CallServerMethod() { var Button1 = document.getElementById("<%=Button1.ClientID %>"); Button1.click(); } Now you can use the javascript "CallServerMethod()"

            I didn't get any requirements for the signature

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            I dunno why someone gave this less than 5, it's the only real way to do it.

            Christian Graus Driven to the arms of OSX by Vista.

            T 1 Reply Last reply
            0
            • S Sundar_R

              document.forms[0].submit() isn't this a simplier way ?

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              Not if he wants to call a specific event, no.

              Christian Graus Driven to the arms of OSX by Vista.

              1 Reply Last reply
              0
              • C Christian Graus

                I dunno why someone gave this less than 5, it's the only real way to do it.

                Christian Graus Driven to the arms of OSX by Vista.

                T Offline
                T Offline
                ToddHileHoffer
                wrote on last edited by
                #7

                Thanks for the support. :)

                I didn't get any requirements for the signature

                1 Reply Last reply
                0
                • T ToddHileHoffer

                  How I usually do this which may not be the best method. Drag a button on your form. Double Click it to get your server side code method. In the pageload hide the button from the user by adding the following: this.Button1.Style.Add("display", "none"); Put your code server side in the button click's event. Then Add the following javascript to your page. function CallServerMethod() { var Button1 = document.getElementById("<%=Button1.ClientID %>"); Button1.click(); } Now you can use the javascript "CallServerMethod()"

                  I didn't get any requirements for the signature

                  A Offline
                  A Offline
                  anderslundsgard
                  wrote on last edited by
                  #8

                  You got my :thumbsup: vote It works perfect for me. About AJAX I luckily easily find a solution using our telerik-framework to avoid the whole page to refresh.

                  _____________________________ ...and justice for all

                  T 1 Reply Last reply
                  0
                  • A anderslundsgard

                    You got my :thumbsup: vote It works perfect for me. About AJAX I luckily easily find a solution using our telerik-framework to avoid the whole page to refresh.

                    _____________________________ ...and justice for all

                    T Offline
                    T Offline
                    ToddHileHoffer
                    wrote on last edited by
                    #9

                    That's funny that you mentioned Telerik. I came up with that solution in order to search the radcombobox. I posted the code Telerik's website. http://www.telerik.com/community/code-library/aspnet-ajax/combobox/searching-in-a-combobox-using-radwindow.aspx[^]

                    I didn't get any requirements for the signature

                    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