Global variable is zero

Dictionary<string, string> propertyCompany = new Dictionary<string, string>();//gloabal variable protected void Page_Load(object sender, EventArgs e) { if(!isPostBack){ propertyCompany .add("a","1"); propertyCompany .add("b","2"); propertyCompany .add("c","3"); } } protected void btnGetProperty_Click(object sender, EventArgs e) { string a=propertyCompany["a"];//error this key is not exist //propertyCompany is null } 

when define a propertyCompany and fill out form_load . clicking propertyCompany is null !?

I use static , but I do not understand, sometime they say that the error is zero.

+4
source share
6 answers

Each request creates a new page object, so you cannot use the dictionary that you created in the first request in the second request (bnt click) (download without postback)

Remove the postback test for a quick fix.

Other fixed features: * store the dictionary in viewstate.

+4
source

Each variable defined in the class that inherits Web.UI.Page will be destroyed at the end of the page life cycle, therefore, it will be null in Postback if you do not reinitialize it.

One way to save it through postbacks is to save it in Session-variable .

You will find a complete list of all options on how to transfer variables through postbacks here: http://msdn.microsoft.com/en-us/magazine/cc300437.aspx

  • Application
  • Cookies
  • Form Field / Hidden Form
  • Querystring
  • Session
  • New state containers in ASP.NET
  • Cache
  • Context
  • ViewState
  • Web.config and Machine.config Files Conclusion

It is in nature an HTTP protocol that it is stateless .

+1
source

I am sure your title should be "a global variable, I do not have the data I want." The dictionary will be built every time the page loads (postback or otherwise), but because of this line:

 if(!isPostBack) { } 

it will not have the necessary data when the button is pressed.

To notify the page of the click, a post-response is executed, therefore !isPostBack (which I assume is installed somewhere through Page.IsPostBack ) also says: “If I didn’t click the button”, which, of course, is not what Do you want to.

To get the necessary functionality, you must either move the dictionary population from this if block, or have an else condition that also fills it with the necessary data.

Another alternative to using a class variable is storing data in another place. Parameters include ViewState , Session , Application (if this is really application data), Cache and some others. It is not clear what exactly the dictionary does, so it’s hard to say which place will be the most suitable.

+1
source

One way to make your dictionary live between requests is to declare it static or in viewstate, as suggested earlier.

0
source

How do you access propertyCompany elements in a button click event? If you do it wrong, this is most likely a problem.

0
source

try it

  Dictionary<string, string> propertyCompany;//gloabal variable protected void Page_Load(object sender, EventArgs e) { propertyCompany = new Dictionary<string, string>(); if(!isPostBack){ propertyCompany .add("a","1"); propertyCompany .add("b","2"); propertyCompany .add("c","3"); } } 

protected void btnGetProperty_Click (object sender, EventArgs e) {// propertyCompany is null}

-1
source

All Articles