如何在会话变量中存储Gridview布尔值并在asp.net中显示另一个页面

时间:2017-11-16 19:40:53

标签: c# asp.net gridview

GridView Page1 CodeBehinde1:

protected void  GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    bool Customs_Clearance = GridView1.SelectedRow.Cells[23].Text;
}

Session["customs_Clearance"] = Customs_Clearance;

和Page2哪个Show会话GridView数据代码背后:     复选框

CheckCustomClearance.Text=Session ["customs_Clearance"].ToString();

1 个答案:

答案 0 :(得分:0)

根据您的情况使用以下代码。 我假设索引为23的列是布尔列,并显示为复选框列。

然后,您可以随时访问其他页面Session["customs_Clearance"]

protected void  GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
   //get selected row
   GridRow  selectedRow = GridView1.Rows[GridView1.SelectedIndex];

   //check if it's a checkbox column and set Session variable, if it is
   if(selectedRow.Cells[23].Controls[0] is CheckBox) {
     bool isChecked = (selectedRow.Cells[23].Controls[0] as CheckBox).Checked;
     Session["customs_Clearance"] = (isChecked == true ? "checked" : "unchecked");
   }
}