菜鸟求助 为何初始化过的HashTable变量 在调试时提示当前上下文中不存在
protected void LinkButton1_Click(object sender, EventArgs e)
{
if (Session[ "购物袋 "] == null)
{ Hashtable hash = new Hashtable(); }
else
{ Hashtable hash = (Hashtable)Session[ "购物袋 "]; };
for (int i = 0; i < GridView1.Rows.Count-1; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl( "CheckBox1 ");
TextBox tb = (TextBox)GridView1.Rows[i].FindControl( "TextBox1 ");
Label lb = (Label)GridView1.Rows[i].FindControl( "TextBox2 ");
if (cb.Checked && Convert.ToInt32(lb.Text) > 0)
{
if (!hash.ContainsKey(lb.Text))
hash[lb.Text] = Convert.ToInt32(tb.Text);
else
hash[lb.Text] = (int)hash[lb.Text] + Convert.ToInt32(tb.Text);
};
}
}
为何调试时会 "当前上下文中不存在名称 'hash ' "??
------解决方案--------------------if (Session[ "购物袋 "] == null)
{ Hashtable hash = new Hashtable();
Session[ "购物袋 "]=hash
}
------解决方案--------------------你在外面建立,到里面去new
protected void LinkButton1_Click(object sender, EventArgs e)
{
Hashtable hash
if (Session[ "购物袋 "] == null)
{ hash = new Hashtable(); }
else
{ Hashtable hash = (Hashtable)Session[ "购物袋 "]; };
------解决方案--------------------同意楼上。
------解决方案--------------------呵呵楼上说的对 .你变量的范围不对
if (Session[ "购物袋 "] == null)
{ Hashtable hash = new Hashtable(); }
else
{ Hashtable hash = (Hashtable)Session[ "购物袋 "]; };
如果session不是null的话,那hash岂不是没定义了吗?
------解决方案--------------------session比较费资源,用viewstate吧,只是在单个页面保留值