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. JavaScript
  4. javascript execute usercontrol method without postback

javascript execute usercontrol method without postback

Scheduled Pinned Locked Moved JavaScript
questionjavascripthelp
15 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.
  • S Offline
    S Offline
    Steve Holdorf
    wrote on last edited by
    #1

    I am trying to call a usercontrol method from within its javascript. Currently, I am using: Code behind: Public bool SaveData() { return true; } Javascript if (counter == 50) '<%= this.SaveData() %>'; Now, the problem is the javascript call to the usercontrol method is called as soon as the page loads. How do I get the usercontrol code behind method to be called when the counter == 50? Thanks, Steve Holdorf

    D D 2 Replies Last reply
    0
    • S Steve Holdorf

      I am trying to call a usercontrol method from within its javascript. Currently, I am using: Code behind: Public bool SaveData() { return true; } Javascript if (counter == 50) '<%= this.SaveData() %>'; Now, the problem is the javascript call to the usercontrol method is called as soon as the page loads. How do I get the usercontrol code behind method to be called when the counter == 50? Thanks, Steve Holdorf

      D Offline
      D Offline
      dusty_dex
      wrote on last edited by
      #2

      Why have you wrapped the <% %> in quotes? a literal string containing ASP delimiters can't be parsed by the server. Please forgive me for asking, as you don't go into much depth with an explanation. but what's wrong with..

      if (counter == 50) this.SaveData();

      "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

      1 Reply Last reply
      0
      • S Steve Holdorf

        I am trying to call a usercontrol method from within its javascript. Currently, I am using: Code behind: Public bool SaveData() { return true; } Javascript if (counter == 50) '<%= this.SaveData() %>'; Now, the problem is the javascript call to the usercontrol method is called as soon as the page loads. How do I get the usercontrol code behind method to be called when the counter == 50? Thanks, Steve Holdorf

        D Offline
        D Offline
        Dennis E White
        wrote on last edited by
        #3

        server code

        [WebMethod]
        public bool Foo()
        {
        return true;
        }

        the in your javascript I would do something like the following (assuming you are using jQuery):

        function callFoo(callBack) {
        $.ajax({
        type: "POST",
        url: "pageName.aspx/Foo",
        data: "{}",
        contentType:"application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
        if(undefined !== callBack) {
        callBack(msg);
        }
        }
        });
        }

        then to change your code I would have the following:

        if(50 === counter) {
            callFoo(function(e) {
                // do something here
            });
        }
        

        I haven't tested this so you will have to veriy it yourself but maybe this will get you started. I am sure there are some articles on this site about calling a WebMethod from javascript. Best of luck.

        as if the facebook, twitter and message boards weren't enough - blogged

        S 1 Reply Last reply
        0
        • D Dennis E White

          server code

          [WebMethod]
          public bool Foo()
          {
          return true;
          }

          the in your javascript I would do something like the following (assuming you are using jQuery):

          function callFoo(callBack) {
          $.ajax({
          type: "POST",
          url: "pageName.aspx/Foo",
          data: "{}",
          contentType:"application/json; charset=utf-8",
          dataType: "json",
          success: function (msg) {
          if(undefined !== callBack) {
          callBack(msg);
          }
          }
          });
          }

          then to change your code I would have the following:

          if(50 === counter) {
              callFoo(function(e) {
                  // do something here
              });
          }
          

          I haven't tested this so you will have to veriy it yourself but maybe this will get you started. I am sure there are some articles on this site about calling a WebMethod from javascript. Best of luck.

          as if the facebook, twitter and message boards weren't enough - blogged

          S Offline
          S Offline
          Steve Holdorf
          wrote on last edited by
          #4

          Thank you very much for your help. One question. Does your example cause a postback? I am trying to call the method without a postback. Thanks, Steve Holdorf

          D 1 Reply Last reply
          0
          • S Steve Holdorf

            Thank you very much for your help. One question. Does your example cause a postback? I am trying to call the method without a postback. Thanks, Steve Holdorf

            D Offline
            D Offline
            Dennis E White
            wrote on last edited by
            #5

            Steve Holdorf wrote:

            One question. Does your example cause a postback?

            the nature of an AJAX call is that the are asynchronous. :-D

            as if the facebook, twitter and message boards weren't enough - blogged

            S 1 Reply Last reply
            0
            • D Dennis E White

              Steve Holdorf wrote:

              One question. Does your example cause a postback?

              the nature of an AJAX call is that the are asynchronous. :-D

              as if the facebook, twitter and message boards weren't enough - blogged

              S Offline
              S Offline
              Steve Holdorf
              wrote on last edited by
              #6

              Again thanks. One problem for me; however, I am using AJAX but not jquery. I still hope there is another solution using just AJAX. Again thanks! Steve Holdorf

              D 1 Reply Last reply
              0
              • S Steve Holdorf

                Again thanks. One problem for me; however, I am using AJAX but not jquery. I still hope there is another solution using just AJAX. Again thanks! Steve Holdorf

                D Offline
                D Offline
                Dennis E White
                wrote on last edited by
                #7

                you can still do it without jQuery however you would have to handcode the call and deal with the various flavors of browsers that are out there. the benefit of jQuery is that encompasses all the nuances of the different browsers for you. I personally would recommend your going the route of using some javascript framework.

                as if the facebook, twitter and message boards weren't enough - blogged

                S 1 Reply Last reply
                0
                • D Dennis E White

                  you can still do it without jQuery however you would have to handcode the call and deal with the various flavors of browsers that are out there. the benefit of jQuery is that encompasses all the nuances of the different browsers for you. I personally would recommend your going the route of using some javascript framework.

                  as if the facebook, twitter and message boards weren't enough - blogged

                  S Offline
                  S Offline
                  Steve Holdorf
                  wrote on last edited by
                  #8

                  Again thanks for sharing your knowledge!!!! I am still a little lost. Can anyone post a small demo or a link to one? Again thanks!!! Steve Holdorf

                  D 1 Reply Last reply
                  0
                  • S Steve Holdorf

                    Again thanks for sharing your knowledge!!!! I am still a little lost. Can anyone post a small demo or a link to one? Again thanks!!! Steve Holdorf

                    D Offline
                    D Offline
                    Dennis E White
                    wrote on last edited by
                    #9

                    there is this article: AJAX for Beginners (Part 2) - Using XMLHttpRequest and jQuery AJAX to implement a cascading dropdown[^]

                    as if the facebook, twitter and message boards weren't enough - blogged

                    S 1 Reply Last reply
                    0
                    • D Dennis E White

                      there is this article: AJAX for Beginners (Part 2) - Using XMLHttpRequest and jQuery AJAX to implement a cascading dropdown[^]

                      as if the facebook, twitter and message boards weren't enough - blogged

                      S Offline
                      S Offline
                      Steve Holdorf
                      wrote on last edited by
                      #10

                      Your link is great but it still uses jquery. Does anyone have a link just using AJAX and javascript? Thanks, Steve Holdorf

                      M 1 Reply Last reply
                      0
                      • S Steve Holdorf

                        Your link is great but it still uses jquery. Does anyone have a link just using AJAX and javascript? Thanks, Steve Holdorf

                        M Offline
                        M Offline
                        Manfred Rudolf Bihy
                        wrote on last edited by
                        #11

                        Then go back again to that article and read a bit more carefully. The article explains how to achieve the same goal using standard AJAX and then also utilizing jQuery. So the answer is there. If you'll look again just search for "Using XMLHttpObject". The part using jQuery is under the heading "Using jQuery". I do think these two shouldn't be too hard to distinguish. Regards,

                        — Manfred

                        "I had the right to remain silent, but I didn't have the ability!"

                        Ron White, Comedian

                        S 2 Replies Last reply
                        0
                        • M Manfred Rudolf Bihy

                          Then go back again to that article and read a bit more carefully. The article explains how to achieve the same goal using standard AJAX and then also utilizing jQuery. So the answer is there. If you'll look again just search for "Using XMLHttpObject". The part using jQuery is under the heading "Using jQuery". I do think these two shouldn't be too hard to distinguish. Regards,

                          — Manfred

                          "I had the right to remain silent, but I didn't have the ability!"

                          Ron White, Comedian

                          S Offline
                          S Offline
                          Steve Holdorf
                          wrote on last edited by
                          #12

                          You are correct. XMLHttpRequest does work for AJAX as well; however, not for what I am doing. Really, all I need to do is to get the usercontrol object (the container control) in my javascript and call the SaveData code behind method (of the container control) without a postback. Thanks for everyones help; however, I am still stuck. Steve Holdorf

                          S 1 Reply Last reply
                          0
                          • S Steve Holdorf

                            You are correct. XMLHttpRequest does work for AJAX as well; however, not for what I am doing. Really, all I need to do is to get the usercontrol object (the container control) in my javascript and call the SaveData code behind method (of the container control) without a postback. Thanks for everyones help; however, I am still stuck. Steve Holdorf

                            S Offline
                            S Offline
                            Steve Holdorf
                            wrote on last edited by
                            #13

                            One thought. I could move the method to the parent page if someone knows how to call a method in the containing page from one of its usercontrols using javascript without a postback. I hope this is also possible. Anyway, with all of the posts everyone has been great! I just got to get it working. Thanks, Steve Holdorf

                            S 1 Reply Last reply
                            0
                            • S Steve Holdorf

                              One thought. I could move the method to the parent page if someone knows how to call a method in the containing page from one of its usercontrols using javascript without a postback. I hope this is also possible. Anyway, with all of the posts everyone has been great! I just got to get it working. Thanks, Steve Holdorf

                              S Offline
                              S Offline
                              Steve Holdorf
                              wrote on last edited by
                              #14

                              OK. I found a way to call the page method from the usercontrol. Now, the problem is PageMethods.SaveData() is only used for static methods. How do I call a non-static page method from my javascript? Thanks, Steve Holdorf

                              1 Reply Last reply
                              0
                              • M Manfred Rudolf Bihy

                                Then go back again to that article and read a bit more carefully. The article explains how to achieve the same goal using standard AJAX and then also utilizing jQuery. So the answer is there. If you'll look again just search for "Using XMLHttpObject". The part using jQuery is under the heading "Using jQuery". I do think these two shouldn't be too hard to distinguish. Regards,

                                — Manfred

                                "I had the right to remain silent, but I didn't have the ability!"

                                Ron White, Comedian

                                S Offline
                                S Offline
                                Steve Holdorf
                                wrote on last edited by
                                #15

                                Manfred, Sorry about the repeated posts. It won't happen again. Thanks, Steve Holdorf :((

                                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