I am trying to fix a real crappy ASP.net 4.0 program handed to me. The people who wrote it did not use the normal asp.net standards. I myself am fairly new to ASP.net and try to learn more about it any chance I can get. In any event it is an ASP.Net 4.0 webforms app with C# code behind.
I am dealing with a page where it has a call to a code behind method in the markup itself. We can call this method get detailById. This method makes a call to a web service which queries the database with this id and returns a dataset to the code behind. Once all this data is received, it is written to the screen in a table. That is it for this method. At the bottom of this page exists various buttons that do different functions. Lets take one button called Hello, for example. All this button has to do is access one of the fields(ex: Username) that were gotten in the detailById method. How do I do make this variable value available to all code behind methods? I considered the following thoughts:
Requery database like detailById did to get this field, but this seems awfully inefficient.
Created a class level variable for the page and set the value of it to the Username variable in the detailById method. This did't work. When I clicked on the "Hello" button, the Username was missing. I know for a fact it was acquired in the detialById method.
Then I thought that postback might be responsible for the value disappearing, so I stored the Username value in a ViewState variable while in the detailById method. When I tried to access this variable it too was null. I have no idea why.
Finally, I put this value in Session while in the detailById method. This worked and the value was able to be referenced in my various code behind methods. This feels like a wrong way to do this. Am I right?
What is the good/correct way to do this?
I hope I made my question clear. Thanks.