Search code examples
c#asp.netstaticpagemethods

How to get controls in static web method


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?
}

Solution

  • 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).