How to access page controls inside a static web method? [duplicate]
Asked Answered
P

2

9

I have called a Code behind method using jQuery using a static WebMethod method.

That web method call was success but when tried to access a text box control it is giving error. An object reference is required for the non-static field, method, or property.

[WebMethod]    
public static Savedata()
 {
     //code to insert data to DB

     //after inserting data successfully i need to change the text box text like following.        
      txtStatus.Text="Data Received";   
 }
Pinafore answered 20/7, 2015 at 10:29 Comment(0)
M
9

As mentioned by @Tim Schmelter This doesn't answer this question because you can't access page's controls from a webmethod.

Please go through asp.net access a control from static function

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.

your question is duplicate of How to get controls in static web method

Marauding answered 20/7, 2015 at 10:35 Comment(5)
here i get Null value..:(Taryn
This doesn't answer this question because you can't access page's controls from a webmethod. So maybe it's helpful for future readers because of the title but it's the wrong answer for this issue.Pantagruel
@Tim Schmelter: you are right.Marauding
Then why it is up-voted ?Oubliette
Wrong! Wrong! Wrong! You can never modify controls' state in service (web method) call.Devitalize
M
6

The accepted answer is wrong for web methods.

As Tim Schmelter mentioned correctly in a comment:

you can't access page's controls from a webmethod

That is true since web methods don't carry the page state. It isn't a full postback. Instead just the session cookie travels with the request. You have to do a full page postback to get or set the control values.

Messina answered 9/2, 2017 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.