我用Hashtable作购物车。但为什么提示GridView1没有的数据源啊?

来源:百度知道 编辑:UC知道 时间:2024/05/07 07:09:02
this.GridView1.DataSource = (System.Collections.Hashtable)Session["bus"];
this.GridView1.DataBind();
实现购手车:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "buy")
{
string Bid = this.DataList1.DataKeys[e.Item.ItemIndex].ToString();
if (Session["bus"] == null)
{
System.Collections.Hashtable ht = new System.Collections.Hashtable();
ht.Add(Bid, 1);
Session["bus"]=ht;
}
else
{
System.Collections.Hashtable ht = (System.Collections.Hashtable)Session["bus"];
if (ht[Bid] == null)
{
ht[Bid] = 1;
}
else
{

检查下初始化时Session["bus"]是否已创建并赋值了,如果不能解决的话,你把具体页面代码贴出来帮你看一下.

asp.net的事件处理顺序中 Page_Load先于控键事件(DataList1_ItemCommand),如果你的this.GridView1.DataSource = (System.Collections.Hashtable)Session["bus"];
this.GridView1.DataBind();
在Page_Load中,就会出现Session["bus"]无值的情况