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
}
当定义一个propertyCompany
并填写form_load
时。单击按钮 propertyCompany
为空!?
我使用 static
但有时我不明白错误为 null。
最佳答案
每个请求都会创建新的页面对象,因此您不能在第二个请求(bnt click)中使用您在第一个请求中创建的字典(无回传加载)
删除回发测试以快速修复。
其他修复可能性: * 在 View 状态中存储字典。
关于c# - 全局变量为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7039833/