0

I have a [WebMethod] and i want to assign values to a text box using the this code:

[WebMethod]
public static void MyMethod(string s)
{
     //TextBox1.Text = s;   //Here how can i access the textbx?
}
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Dhaval Shukla
  • 1,127
  • 2
  • 10
  • 19
  • Duplicate question .please go through this post http://stackoverflow.com/questions/2133194/access-asp-net-control-from-static-webmethod-js-ajax-call – Devjosh Jan 23 '12 at 06:08

2 Answers2

15

You can't.

The whole point of [WebMethod]s is that they don't run the ASP.Net page lifecycle. This way, they're fast and parallelizable.
Your controls don't exist.

Instead, you should use Javascript (better) or an UpdatePanel (worse).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • If I'm stuck using a web method (there's a c# library that's crucial), is there a way to access the page's markup to do something like send an alert that something has happened? – Justin L. Jan 18 '16 at 19:22
1

in case of static method(ie, page method asynchronous call), whole page is not posted back..and hence there is no information on server about the page controls(textbox or whatever)..

server doesn't retain the state of any of the controls or so while rendering unless made to do so(session or any other state management)..

So if you want to work one the values of the page controls, send such information in the asynchronous request itself and work on those values and return the response..and assign it to respective controls back in client side script..

techBeginner
  • 3,792
  • 11
  • 43
  • 59