簡體   English   中英

如何在頁面之間傳遞值?

[英]How to Pass Values Between Pages?

我正在使用在C#中使用帶有ASP.NET的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);
    }
}  

請幫助我,謝謝。

有幾種方法可以做到這一點:

詢問

// 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文章以查看更多示例和詳細說明。

大概seatnum是一個字符串。

如果是這樣,請嘗試

totalseatnum1 = firstseatnum + Convert.ToInt32(seatnum);

totalseatnum2 = economyseatnum + Convert.ToInt32(seatnum);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM