Click here to Skip to main content
16,022,260 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!
You can create variables as public shared y class section

Public Shared testvaraible As String

You can move value in some place of the page. Example in page load testvariable="test"

Now in the webmethod you can access the value
Example dim s as string=testvariable
 
Share this answer
 
Comments
Richard Deeming 13-Sep-24 11:43am    
Aside from the fact that you're 13 years too late, this is a terrible suggestion.

Firstly, the OP wanted to access controls from a static method. You cannot make the controls on a WebForms page static, since it doesn't work like that.

Secondly, using any static state in a web application is almost always the wrong solution to the wrong problem. The state will be shared across every request from every user. Aside from thread-safety issues, you run the very real risk of leaking state between requests and between users.

So your answer is not only far too late, it's dangerous.
Noreen Gonz Mar 13-Sep-24 15:20pm    
I am a programmer with 24 years of experience. It worked for me and I wanted to share it with others who need it. Not everyone has the necessary knowledge, I feel for you. Please look for another complaint tool, this one is to help.
Dave Kreskowiak 13-Sep-24 18:40pm    
Well, I raise you my 46 years of writing code. Richard is correct. Your solution, though it worked for YOU AND ONLY YOU, does not work when more than one user is on the site at the same time. You ARE leaking state between sessions since static variables are shared between all instances.

There is a massive difference between actually helping someone with a good solution and hindering someone with a bad solution.

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