【问题标题】:Gridview Session value is nullGridview 会话值为空
【发布时间】:2013-10-08 11:26:49
【问题描述】:

我想在一页上有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;   
             }

在另一个页面上,我想获取 session 的值,所以我在另一个页面上做了如下操作

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

        }
    }

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

我不明白哪里错了?

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

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

【问题讨论】:

  • BTW chk_CheckedChanged 事件是否触发?
  • 是的 chk_CheckedChanged 事件正在触发....
  • 不重复的问题....但是它们在会话中没有价值.....下面你可以看到代码
  • protected void chk_CheckedChanged(object sender, EventArgs e) { GridViewRow row = ((GridViewRow)((Control)sender).Parent.Parent);字符串 cellvalue = row.Cells[1].Text;会话 [“pId”] = 单元格值;

标签: c# asp.net session gridview nullreferenceexception


【解决方案1】:
 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;    

    }

开启 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 
        { 

        }
    }

更新

抱歉,我错过了您的部分问题,作为您的网格视图,您使用了 BoundField,因此您可以将单元格值获取为 row.Cells[index].text

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

--希望对你有帮助

【讨论】:

  • thx...我尝试了很多您的代码....但我收到错误:索引超出范围。必须是非负数且小于行字符串requestid = gvDoctorList.DataKeys[row.RowIndex].Value.ToString();............我的代码如下.....
  • protected void chk_CheckedChanged(object sender, EventArgs e) { GridViewRow row = ((GridViewRow)((Control)sender).Parent.Parent); string requestid = gvDoctorList.DataKeys[row.RowIndex].Value.ToString();字符串 cellvalue = row.Cells[1].Text;会话 [“pId”] = 单元格值; }
  • 我必须删除行字符串 requestid = gvDoctorList.DataKeys[row.RowIndex].Value.ToString();
  • 我检查过 session 的值是空的....有什么问题我不明白....没有错误...但是 session 仍然没有值?跨度>
  • 首先检查一下,当您分配会话时,您确定它包含所需的值,我的意思是您正在获得PatientID 值?
【解决方案2】:

您不是在为键“pId”设置值并尝试访问其他键“patientid”吗?使用相同的键来设置和获取 Session 的值。

【讨论】:

    【解决方案3】:

    我不知道您的复选框是如何触发的,没有 AutoPostback="True"。此外,每次回发都会丢失数据源。您可以将 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?

    【讨论】:

    • 不,我已将 AutoPostBack="true" 放在复选框中...但字符串 cid = rows.Cells[1].Text; 中仍然没有值;.....出了什么问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2019-08-29
    • 2020-11-09
    • 2016-10-15
    • 1970-01-01
    相关资源
    最近更新 更多