如何通过会话将值从gridview传递到另一个页面?

时间:2013-07-24 13:36:57

标签: c# asp.net .net

我想在按下按钮时,在会话的帮助下将单个值从网格视图传递到另一个页面。怎么能实现呢?

c#c​​ode:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindEmployeeDetails();
    }
}

protected void BindEmployeeDetails()
{
    con.Open();
    SqlCommand cmd = new SqlCommand("Select uniqueref,fldCustomerName,fldCustomerAddress,fldCustomerCity,fldOrderValue,fldINRrEUR from asm", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    if (ds.Tables[0].Rows.Count > 0)
    {
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    else
    {
        ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
        GridView1.DataSource = ds;
        GridView1.DataBind();
        int columncount = GridView1.Rows[0].Cells.Count;
        GridView1.Rows[0].Cells.Clear();
        GridView1.Rows[0].Cells.Add(new TableCell());
        GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
        GridView1.Rows[0].Cells[0].Text = "No Records Found";
    }
}

protected void Add_Click(object sender, EventArgs e)
{
    Response.Redirect("add.aspx");
}

protected void Edit_Click(object sender, EventArgs e)
{
    Response.Redirect("edit.aspx");
}

1 个答案:

答案 0 :(得分:1)

将值设置为Session,如下所示:

// We will say that `GridView1.PrimaryKey` is an int 
Session["YourGridValue"] = GridView1.Value;

Session读取值,如下所示:

   // Check first to make sure our value is in Session
   if(null != Session["YourGridValue"])
   {
        int sessionValue = (int)Session["YourGridValue"];
   }

注意:Session中的值存储为Object,但可以是字符串,整数,列表等;因此,在检索值时必须将其强制转换为正确的类型。