protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "安排")
        {
            Session["Ship_id"] = e.CommandArgument;
            
            Session["Ship_Item_style"] =  GridView1.SelectedRow.Cells[1].Text;          
           
            Session["Ship_Goal_harbour"] =  GridView1.SelectedRow.Cells[5].Text;
            Response.Redirect("~/Ship_decide_2.aspx");
        }
    }我运行后点击gridview的安排之后提示 
Session["Ship_Item_style"] =GridView1.SelectedRow.Cells[1].Text;  这行未将对象引用设置到对象的实例,但是我网页里有个GridView1了,还要初始化什么吗?

解决方案 »

  1.   


    Ship_id    Item_style   Max_weight Now_weight   Now_harbour  Goal_harbour    
    bh22222222   集装箱    2000          1200       beihai    qinzhou 查看货物  安排我运行后的画面大概是这样,“查看货物”是gridview原来的“选择”,“安排”是我编辑模板加的linkbutton,如果先点“查看货物”再按“安排”是正常的,但是如果我先按“安排”就提示出错了。怎么回事呢?
      

  2.   

    原因可能是Session没有"Ship_Item_style"项,或者GridView1没有选中行,或者选中行的Cells[1]为空。
      

  3.   

    cells[i] i是从零开始的,或许cells[1]为null
      

  4.   

    可是Session不是不用new的吗?我是点击了GridView1行尾的linkbutton后出错的,按理说应该选中行了呀。Cells[1]像我刚刚在2楼发的数据是有的,值是“集装箱”。
      

  5.   

    Ship_id    Item_style   Max_weight Now_weight   Now_harbour  Goal_harbour    
    bh22222222   集装箱    2000          1200       beihai    qinzhou 查看货物  安排
    我的数据是这样的,按理说cells[1]应该是“集装箱”呀,不是空的呀。