0

これは、XMLページから取得しているページで、Cookieに保存することで、別のページで取得したいと考えています。

public partial class shopping : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpCookie userCookie = new HttpCookie("user");
        userCookie["quantity"] = TextBox1.Text;

        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("shopping_cart.xml"));
        XmlNode root = doc.DocumentElement;

        if (RadioButton1.Checked)
        {
            string str1 = doc.GetElementsByTagName("cost").Item(0).InnerText;
            userCookie["cost"] = str1;
            //Label3.Text = str1;
            Response.Redirect("total.aspx");
        }

    }
}

そして、ここに私がそれを取得しようとしている他のページがあります(total.aspx.cs):

public partial class total : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        **Label2.Text = Request.Cookies["user"]["quantity"];**

    }
}

太字の行で Null 参照を取得しています。どうすればそれを行うことができますか?

4

1 に答える 1

2

最初のセクションで Cookie を作成しましたが、Response.

 Response.Cookies.Add(userCookie); // place before your Response.Redirect

また、Cookie の有効な最大サイズは 4000 バイトであり、それ以外の場合はおそらく最適な選択ではないことに注意してください。SessionCookie を使用するのではなく、ページ間のアクセスのために一時的なセッション情報を に保存したい場合があります。

 Session["quantity"] = TextBox1.Text

 // ...

 Session["cost"] = str1;

そして2ページ目に

Label2.Text = Session["quantity"] as string;
于 2012-06-12T07:20:41.423 に答える