javascript execute usercontrol method without postback
-
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
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.
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
Your link is great but it still uses jquery. Does anyone have a link just using AJAX and javascript? Thanks, Steve Holdorf
-
Your link is great but it still uses jquery. Does anyone have a link just using AJAX and javascript? Thanks, Steve Holdorf
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
-
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
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
-
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
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
-
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
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
-
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
Manfred, Sorry about the repeated posts. It won't happen again. Thanks, Steve Holdorf :((