如何通过会话将值从gridview传递到另一个页面
本文关键字:另一个 gridview 会话 何通过 | 更新日期: 2023-09-27 18:24:38
我想在按下按钮时借助会话将一个值从网格视图传递到另一个页面。如何实现?
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");
}
将值设置为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
,但可以是字符串、int、List等。;因此,在检索值时必须将其强制转换为正确的类型。