0

こんにちは、非常に単純な Asp.net アプリケーション プロジェクトを行っています。

 namespace WebApplication1
 {
 public partial class WebUserControl1 : System.Web.UI.UserControl
 {
    market m = new market();

    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void button_clickSell(object sender, EventArgs e) 
    {


        float price = float.Parse(this.BoxIdPrezzo.Text);

        m.insertProd("xxx", 10, "yyy");
        m.addOfferForProd("ooo", 5, "gggg");
        m.insertProd(this.BoxIdDescrizione.Text,price,this.BoxIdUtente.Text);
        String s;
        m.outMarket(out s);  
        this.Output.Text = s;  //the output here work good
        this.Output.Visible = true;

    }
    protected void button_clickView(object sender, EventArgs e) 
    {
        String s;
        m.outMarket(out s);
        this.Output.Text = s;  // here seem to have lost the reference to product why?
        this.Output.Visible = true;
    }
}
}

問題は、button_clickSell を呼び出す button1 をクリックするとすべて正常に動作しますが、button_clickView 製品を呼び出す button2 をクリックすると、Market オブジェクトにはもうないように見えますが、Market オブジェクトには製品のリストがあるため、これはかなり奇妙です。と m.outMarket は初めて適切に機能します。

4

3 に答える 3

4

これは、ページの仕組みによるものです。ページへのリクエストまたはポストバックを行うたびに、その変数の値が失われます。

セッションなどでそれを保持する必要があります。

これは、セッションを使用する非常に基本的な例です。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Collection"] == null)
        {
            Session["Collection"] = new List<int>();
        }//if
    }
    protected void button_clickSell(object sender, EventArgs e)
    {
        List<int> collection = (List<int>)Session["Collection"];
        collection.Add(7);
        collection.Add(9);
    }
    protected void button_clickView(object sender, EventArgs e)
    {
        List<int> collection = (List<int>)Session["Collection"];
        collection.Add(10);
    }
于 2012-04-01T07:28:50.960 に答える
0

この投稿はMSDNで表示できます:ASP.NETセッション状態の概要

于 2012-04-01T07:45:01.963 に答える
0

Sessionページ全体に情報が必要な場合に使用する必要があります。次に、同じページにある 2 つのボタンの問題です。したがって、ViewState が最適なオプションです。

protected void Page_Load(object sender, EventArgs e)
{
     if (ViewState["Collection"] == null)
     {
            ViewState["Collection"] = new List<int>();
     }//if
}
protected void button_clickSell(object sender, EventArgs e)
{
     List<int> collection = (List<int>)ViewState["Collection"];
     collection.Add(7);
     collection.Add(9);
}
protected void button_clickView(object sender, EventArgs e)
{
     List<int> collection = (List<int>)ViewState["Collection"];
     collection.Add(10);
}
于 2012-04-01T08:41:11.760 に答える