Gridview会话值为null

时间:2013-10-08 11:26:50

标签: c# asp.net session gridview nullreferenceexception

我希望在一个页面上显示gridview,如下所示

<asp:GridView ID="gvDoctorList" runat="server" 
           AutoGenerateColumns="False" 
           DataSourceID="SqlDataSource1" 
           AllowPaging="True" 
           AllowSorting="True" 
           AutoGenerateEditButton="true" 
           AutoGenerateDeleteButton="true">
     <Columns>
         <asp:TemplateField>
              <ItemTemplate>
                   <asp:CheckBox runat="server" 
                                 ID="chk" 
                                 OnCheckedChanged="chk_CheckedChanged"
                                 AutoPostBack="true"/>
                   <asp:Label runat="server" ID="lblPID" Visible="false" Text='<%# Eval("PatientId") %>'></asp:Label>
              </ItemTemplate>
         </asp:TemplateField>
         <asp:BoundField DataField="PatientId" 
                         HeaderText="PatientId" 
                         SortExpression="PatientId" />
         <asp:BoundField DataField="firstname" 
                         HeaderText="firstname" 
                         SortExpression="firstname" />
         <asp:BoundField DataField="lastname" 
                         HeaderText="lastname" 
                         SortExpression="lastname" />
          <asp:BoundField DataField="sex" 
                         HeaderText="sex" 
                         SortExpression="sex" />
     </Columns>
  </asp:GridView>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                     ConnectionString="<%$ ConnectionStrings:MyDatabaseConnectionString %>"  
                     SelectCommand="SELECT [PatientId],[firstname], [lastname], [sex] FROM [PatientDetails]">
  </asp:SqlDataSource>

现在我将会话放在复选框更改事件上,如下所示

 protected void chk_CheckedChanged(object sender, EventArgs e)
 {
         GridViewRow row = ((GridViewRow)((Control)sender).Parent.Parent);
        string requestid =  gvDoctorList.DataKeys[row.RowIndex].Value.ToString();
        string cellvalue = row.Cells[1].Text;
        Session["pId"] = cellvalue;   
             }

在另一个页面上,我想获得会话的价值,所以我在另一个页面上做了一些事情,如下所示

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["pId"] != null)
        {
            lblsession.Text = Session["pId"].ToString();
        }
        else 
        { 

        }
    }

但问题是,当我调试复选框更改事件上的代码时,特别是在会话行上,它会向我显示错误,即Object引用未设置为对象的实例。

我不明白有什么不对?

我想要做的是从gridview获取patientid的值,用户已经检查了复选框....将所选值发送到另一个页面,并希望将patientid插入到sql表中。

请提供编码或其他好主意的任何例子。

3 个答案:

答案 0 :(得分:0)

您是不是为关键字“pId”设置了值并试图访问其他一些关键字“patientid”?使用相同的键设置并从Session获取值。

答案 1 :(得分:0)

 protected void chk_CheckedChanged(object sender, EventArgs e)
    {
        GridViewRow Row = ((GridViewRow)((Control)sender).Parent.Parent);
       // string requestid = grdrequestlist.DataKeys[Row.RowIndex].Value.ToString();
        string cellvalue=Row.Cells[1].Text;
        Session["pId"] =cellvalue;    

    }

ON Page_Load事件

protected void Page_Load(object sender, EventArgs e)
    {
        //if (Session["patientid"] != null) you have defined pID as session variable 
        // so check for pID not patientid
       string patientID =Convert.Tostring(Session["pId"]);
         if(!string.IsNullOrEmpty(patientID ))
        {
            lblsession.Text = patientID;
                  //Session["pId"].ToString();
        }
        else 
        { 

        }
    }

<强>更新

抱歉,我错过了你的一些问题,作为你的gridview,你已经使用了BoundField,所以你可以获取单元格值 row.Cells [索引]的.text

string PatientID= row.Cells[0].Text;
Session ["pID"] = PatientID;

- 希望有所帮助

答案 2 :(得分:0)

我不知道您的复选框是如何触发的,没有AutoPostback =“True”。此外,Datasource每次回发都会丢失。您可以将Id存储在隐藏字段或复选框的value属性中,以便在事件中检索它。

编辑:请参阅以下链接:http://www.daveparslow.com/2007/08/assigning-value-to-aspnet-checkbox.html Why I can't set a value on a asp:CheckBox?