如何在页之间传递值
本文关键字:之间 | 更新日期: 2023-09-27 18:16:50
我正在做航空公司预订系统使用GUI与ASP。. NET中的c#。我想要的是为用户分配座位,在用户分配座位后,该座位不能再被其他人分配。我尝试使用增量这样做,但不是1+1= 2(这意味着座位号2是下一个要分配的),系统给我1+1=11。我怎样才能做我想做的?
我为此有5个接口,我需要在整个运行过程中存储这个赋值。这是我预订按钮的代码,我有2个,头等舱和经济舱。头等舱只有5个座位,经济舱只有15个座位。我该怎么办?
protected void Button1_Click(object sender, EventArgs e)
{
Session["seats"] = "First Class";
if (firstseatnum > 5)
{
MessageBox.Show("Sorry, the first class seats was fully booked. Can you change to economy class?");
Response.Redirect("reservation.aspx");
}
else
{
++firstseatnum;
totalseatnum1 = Convert.ToInt32(firstseatnum+seatnum);
Response.Redirect("~/information.aspx?firstseatnum="+firstseatnum+"&seatnum="+totalseatnum1);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Session["seats"] = "Economy Class";
if (economyseatnum > 20)
{
MessageBox.Show("Sorry, the economy class seats was fully booked. Can you change to first class?");
Response.Redirect("reservation.aspx");
}
else
{
++economyseatnum;
totalseatnum2 = Convert.ToInt32(economyseatnum + seatnum);
Response.Redirect("information.aspx?economyseatnum=" + economyseatnum + "&seatnum="+totalseatnum2);
}
}
请帮帮我,谢谢。
有几种方法:
查询// Set
var myVariable = "MyData";
Response.Redirect("/NextPage?MyVariable=" + myVariable);
// Get
var data = Request.QueryString["MyVariable"];
饼干
// Set
var cookie = new HttpCookie("MyVariable");
cookie.Value = "NyData";
Response.Cookies.Add(cookie);
Response.Redirect("/NextPage");
// Get
var data = Request.Cookies["MyVariable"].Value;
<<p> 会话/strong> // Set
Session["MyVariable"] = "MyData";
Response.Redirect("/NextPage");
// Get
var data = Session["MyVariable"];
Application (Store whole Process)
// Set
Application["MyVariable"] = "MyData";
Response.Redirect("/NextPage");
// Get
var data = Application["MyVariable"];
还可以查看CodeProject文章以查看更多示例和详细解释。
假设seatnum
是一个字符串。
如果是,试试
totalseatnum1 = firstseatnum + Convert.ToInt32(seatnum);
和
totalseatnum2 = economyseatnum + Convert.ToInt32(seatnum);