首页 文章

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

提问于
浏览
-3

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

c#代码:

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 回答

  • 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 ,但可以是字符串,整数,列表等;因此,在检索值时必须将其强制转换为正确的类型 .

相关问题