I would like to take one decision based on the question described. I had already used PageMethods in clientside code. If it triggers postback then I wanted to replace it with ajax call from client side. Can anybody clarify it?
CodePudding user response:
A web method of the given page when called by a ajax call client side does NOT cause a post-back. But, keep in mind that such web methods can't referance any control on the page, nor can you use ViewState. You can use session() in a web method.
Often, in some cases, you can then adopt a update panel, and put the controls in that update panel, and thus when you click a button, or any event code from a control?
Then you don't get nor see a full page post-back. So often, a update paneal is a great way to prevent and not have full page post back. But, do keep in mind that a update panel DOES cause a partial page post-back, and even your page load event fires again.
But, a web method? No page post back occures. You often require this since a jQuery.UI pop dialog can disply things, but if that dialog needs server side code, then you can't have a post-back (since then the dialog will collapse, since you getting a new fresh re-rendered page. And even using a up-date panel will also collapse a dialog.
however, if you don't have some pop dialog showing? Then it MUCH less work and effort to just use a update panel, and not bother with all the work to wire up a ajax call to a web method.
So, in most cases I don't make the time effort to build a ajax call.
So, look into a update panel first - often less work.
But, if you just need some value from the server, don't want a post-back, and don't want (or can't use a update-panel), then a web method is great choice. (since no post-back occurs). The big down side of a web method is no use of controls or anything from the web page can be used in that ajax call to the web method (so, you have to have the data you need client side, or pass some value(s) to the server side web method.
A pagemethod also does not cause a post-back.