Click here to Skip to main content
15,886,007 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
In my aspx page i have static method.
I am not able to access any controls from static method
How can i access the controls inside that page

Thanks in advance,
Posted
Comments
Philippe Mori 28-Oct-15 13:17pm    
Generally a bad idea. Why would you want to do that? If you wrote the function the why have you made it static and if not, then probably that function is not expected to use a control...

How can you access some members from a non-static (instance) method? Never though about it? It it possible because the instance (object) having those instance members is passed to the call in the form instance.method(). In fact, instance reference is passed as an implicit parameter to method().

Are you getting a hint? All you need it to add an explicit parameter of the class type to the method and do nearly the same. But why doing it if there are instance methods? In some relatively rare cases it can be useful.

—SA
 
Share this answer
 
Comments
CPallini 28-Oct-15 4:50am    
5
Sergey Alexandrovich Kryukov 28-Oct-15 9:53am    
Thank you, Carlo.
—SA
If you are using Asp.net then, You can access page controls (like asp.net access a control) from static function.

CSS
public static void Savedata()
{
    if (HttpContext.Current != null)
    {
        Page page = (Page)HttpContext.Current.Handler;
        TextBox TextBox1 = (TextBox)page.FindControl("TextBox1");

        TextBox TextBox2 = (TextBox)page.FindControl("TextBox2");
    }
}


Above method is for finding the control values. 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).
 
Share this answer
 
v2
you cant access nonstatic controls from a static method.
static methods can access static variables only..

if my answer is not correct then dont forgot to intimate me.... just leave a comment
 
Share this answer
 
v2
Comments
RaisKazi 27-Aug-11 3:12am    
Agree. 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900