Click here to Skip to main content
12,690,140 members (28,256 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
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 26-Aug-11 20:32pm
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...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
CPallini 28-Oct-15 4:50am
   
5
   
Thank you, Carlo.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

If you are using Asp.net then, You can access page controls (like asp.net access a control) from static function.

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).
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170117.1 | Last Updated 28 Oct 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100