0

hello i m doing a very simple Asp.net application project

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

the problem is that when i click on button1 which call button_clickSell everything works good but when i click on button2 which call button_clickView products seem to not be anymore in the Market object, but this is pretty strange because in market object i have a list of product and m.outMarket in the first time work propely.

FrankTan
  • 1,626
  • 6
  • 28
  • 63

3 Answers3

4

That is because of how pages work. Every time you make a request or a post-back to the page the values will be lost in that variable.

You will need to hold that in a session or something similar.

Here is a very basic example of using a session.

    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);
    }
Brian
  • 1,164
  • 1
  • 9
  • 27
  • ok please can you make an example ? how do i store them in a session ? point me to a link ? – FrankTan Apr 01 '12 at 07:30
  • I added a basic example of a session. You should read up on them a bit more if you have never seen them before. – Brian Apr 01 '12 at 07:37
  • i think it should be like this.. http://stackoverflow.com/questions/6380842/asp-net-do-changes-to-session-objects-persist – FrankTan Apr 01 '12 at 07:39
  • if i store everything in a databse it is odd or it makes sense ? – FrankTan Apr 01 '12 at 07:58
  • I can't really answer that. Depends on how you want to build your application. I can say it is very normal to store data in a database for a web based application. – Brian Apr 01 '12 at 08:04
0

you can view this post on MSDN: ASP.NET Session State Overview

Dozer
  • 5,025
  • 11
  • 36
  • 52
0

Session should be used when information is required across the pages. Now the matter for the two buttons lying on the same page. So ViewState is Best option.

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);
}
Pankaj
  • 9,749
  • 32
  • 139
  • 283