-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");
}
4

1 回答 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"];
   }

注意: in 中的值Session存储为Object,但可以是字符串、int、List 等;因此在检索值时必须将其转换为正确的类型。

于 2013-07-24T13:46:17.637 回答