如何在页面之间传递值?

时间:2016-11-06 14:50:20

标签: c# asp.net user-interface

我正在使用GUI在C#中使用GUI进行航空公司预订系统。 我想要的是为用户分配座位,在用户分配该座位后,该座位不能再被其他人分配。我尝试使用增量执行此操作,但不是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);
    }
}  

请帮帮我,谢谢。

2 个答案:

答案 0 :(得分:3)

有几种方法可以做到:

<强>查询

// 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;

<强>会话

// Set
Session["MyVariable"] = "MyData";
Response.Redirect("/NextPage");

// Get
var data = Session["MyVariable"];

申请(存储整个流程)

// Set
Application["MyVariable"] = "MyData";
Response.Redirect("/NextPage");

// Get
var data = Application["MyVariable"];

同时检查CodeProject article以查看更多示例和详细说明。

答案 1 :(得分:1)

据推测,seatnum是一个字符串。

如果是,请尝试

totalseatnum1 = firstseatnum + Convert.ToInt32(seatnum);

totalseatnum2 = economyseatnum + Convert.ToInt32(seatnum);