-1

I'm starting to believe there is no way for me to interact with asp controls from within a webmethod. I wouldve thought i could some how accomplish this, by finding the page the webmethod was called from, and from there find and update controls on the page.

But after having an open question for 3 days and numerous google searches, it seems that there is no way for me to do this. Or is there? I would really appreciate if someone could provide some useful information for me on this matter.

The problem is quite simple from a perspective.

 1. I'm calling a webmethod through ajax, this is happening on say page A.
 2. After a succesful ajax call, i wish to update an ASP control on page A.
 3. The update of the ASP control must happen without a postback hence the ajax.

Is this really impossible? Also if you know anything about this matter, but you don't understand my question, please have a look at my other active question: Update object on masterpage through ajax webmethod

Mathias
  • 119
  • 1
  • 2
  • 16

2 Answers2

1

When you call a webmethod, what happens is quite different compared to a button click for example in asp.net webforms.

The webmethod doesn't construct all the controls as a standard click event does.

So that's why you can't have access to the page controls.

Also, how would this work even if you had that access? Your webmethod only sends back some data, not the entire html code, so there's no way to update a control's value server side, since it has to be rendered in html.

If you want to update the value of a control on the client side (webbrowser), you can only do it via javascript when you receive the result of your webmethod. You only have to find the control by its id, and update its value.

For more information, you can look at this post: What's the ASP.NET Webservice request lifecycle?

Another way to achieve what you want to do is to use the UpdatePanel. I personally don't like it, but it lets you access all the controls that are inside it, and update their values. This control takes care of the client side update via javascript (but it actually replaces big parts of html in the page so it might be quite slow)

Community
  • 1
  • 1
ppetrov
  • 3,077
  • 2
  • 15
  • 27
  • Thank you for the answer, it certainly also made me more aware of what is happening when using webmethods. I'm not a big fan of updatepanels, but in this case it seems like i would have to use it. But say i wrap a updatepanel around a repeater, how could use jquery to for a postback on the updatepanel? – Mathias Mar 09 '14 at 18:25
  • If the button that makes the postback is inside the updatepanel you don't have to use javascript, if not: http://stackoverflow.com/questions/15928822/force-a-postback-in-javascript-for-updatepanel – ppetrov Mar 09 '14 at 21:28
0

Calling a web method via AJAX has no impact on the HTML that has already been rendered to the browser.

What you'll need to do is return some information from your web method, and when the AJAX call completes, use jQuery to modify the appearance of the screen using the new information.

If what you're doing in the web method would result in a big change, one that you couldn't easily make happen in jQuery (such as re-rendering a GridView) you might want to look at UpdatePanels.

Ann L.
  • 13,760
  • 5
  • 35
  • 66
  • Thank you for the answer. If it was a matter of a simple html correction i wouldve done it in seconds and be done with it. It is in fact a repeater that i need to update after the webmethod has been executed. – Mathias Mar 09 '14 at 18:23
  • I've posted a suggestion to your other question. – Ann L. Mar 09 '14 at 18:28