开发者

global variable is null

Dictionary<string, string> propertyCompany = new Dictionary<string, string&g开发者_开发知识库t;();//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 in form_load. in on click button propertyCompany is null!?

i use a static but i does not understand sometime say error is null.


Each request creates new page object, therefore you cannot use in second request (bnt click) dictionary you have created in first request (load without postback)

Remove test for postback for quick fix.

Other fix posibilities: * store dictionary in viewstate.


Every variable defined in a class inheriting Web.UI.Page will be destroyed at the end of the Page-Lifecycle, hence it will be null in a Postback if you don't reinitialize it.

One way to persist it across postbacks is to store it in a Session-variable.

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

  • Application
  • Cookies
  • Form Post / Hidden Form Field
  • QueryString
  • Session
  • New State Containers in ASP.NET
  • Cache
  • Context
  • ViewState
  • Web.config and Machine.config Files Conclusion

It's in the nature of HTTP-protocol that it is stateless.


I'm pretty sure your title should be "global variable does not have the data I want." The dictionary will be constructed each time the page is loaded (postback or otherwise), but because of this line:

if(!isPostBack) {
}

it won't have the data you want on a button click.

In order to notify the page of the click, a post back is performed, so saying !isPostBack (which I'm assuming is set somewhere via Page.IsPostBack) is also saying "if I haven't clicked the button", which is of course not what you want.

In order to get the functionality you want, you should either move the population of the dictionary out of that if block, or else have an else condition that also populates it with data you want.

Another alternative to using the class variable is to store the data in another location. Options include ViewState, Session, Application (if it really is application-wide data), the Cache, and some others as well. It's not clear exactly what the dictionary is doing, so it's hard to say which location would be most appropriate.


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


How are you accessing the items in propertyCompany in the button click event? If you are doing that incorrectly, that is more likely the issue.


try this

    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 }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜