简体   繁体   中英

Transfer data (in List generic) without Session ViewState or whatelse. how to?

i have 3 Array X,Y,Risk. i added a list generic (listtraining). if i click buttun Run my codes. But i dont want to use Session to transfer data. in winforms i dont need Session. May List collect data in his memory?

 public partial class _Default : System.Web.UI.Page
    {
        double[] X;
        double[] Y;
        string[] Risk;
        List<KNNAlgorithms.TrainigSet> listtraining;
        protected void Page_Load(object sender, EventArgs e)
        {
            listtraining = new List<KNNAlgorithms.TrainigSet>();
            if (!IsPostBack)
            {


                X = new double[] { 0.08, 0.07, 0.20, 1.00, 0.05, 0.20, 0.17, 0.15, 0.50, 0.10 };
                Y = new double[] { 0.20, 0.07, 0.09, 0.20, 0.06, 0.25, 0.07, 0.55, 0.08, 0.06 };
                Risk = new string[] { "erkek", "erkek", "erkek", "kadın", "erkek", "erkek", "erkek", "kadın", "erkek", "kadın" };

                for (int i = 0; i < X.Length; i++)
                {
                    KNNAlgorithms.TrainigSet tr = new KNNAlgorithms.TrainigSet();

                    tr.X = X[i];
                    tr.Y = Y[i];
                    tr.Risk = Risk[i];
                    listtraining.Add(tr);
                }
                Session.Add("listtraining", listtraining);
                for (int i = 0; i < listtraining.Count; i++)
                {
                    ListBox1.Items.Add(listtraining[i].X.ToString());
                    ListBox2.Items.Add(listtraining[i].Y.ToString());
                    ListBox3.Items.Add(listtraining[i].Risk.ToString());
                }
            }
        }

        protected void btnHesapla_Click(object sender, EventArgs e)
        {
            List<KNNAlgorithms.TrainigSet> listtraining = new List<KNNAlgorithms.TrainigSet>();

            KNNAlgorithms.KNNModel knn = new KNNAlgorithms.KNNModel();
            double x = double.Parse(txtX.Text);
            double y = double.Parse(txtY.Text);
            int k = int.Parse(txtKValue.Text);
            listtraining = (List < KNNAlgorithms.TrainigSet > )Session["listtraining"];
            lblResult.Text = "Üretilen Sonuç:  " + knn.CalculatedDistancesArray(listtraining, x, y, "kadın", "erkek", k);
        }
    }

The list data needs to be persisted some where and your options on the web are session, viewstate, cache. The data stored in any of these mediums is actually stored in server's memory.

The other option is that you serialize the data into XML or plain text [as it may suit you] and store it as a file on the server. This is generally not a good approach however coz you will end up with umpteen files on the file server.

One more option is to serialize the data, store it in a hidden input element on the form and pass it from page to page where you need it. This will be slightly cumbersome to code & handle overall.

The best bet i woudl still recommend as a session variable.

In ASP.Net storing data on a page during postbacks can be done using session variables, viewstate or hidden fields.

The only way to transfer data between pages is using either a session object, or the query string or POST variables. This means you will need to either pass it in a session or reload the list on each page load.

Therefore the answer to your question is no, the list will not be stored in memory once the pages is loaded. I would suggest you reload the list on each postback if required, or alternatively store it in viewstate if you don't want to use a session variable. However keep in mind that using viewstate adds to the size of the returned page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM