简体   繁体   中英

Accessing list objects from another asp.net page

I create a list ...

private static List<int[]> arrayList = new List<int[]>();

public static void Add(int Id, int Quantity)
{
    int[] buying = new int[] {Id, Quantity};
    arrayList.Add(buying);
}

Now ideally I want to access these list objects from another page, but its the first time I've really used lists and arrays so I don't know where to start?

any pointers?

Thanks

Save the objects's state from one page in Session:

Session["arrayList"] = arrayList

Access it in another Page:

if(Session["arrayList"]!=null)
 List<int[]> arrayList = Session["arrayList"] as List<int[]>;

1) Your List is private. U must do it public 2) Where is this code located?

You can expose another method like so

public static IEnumerable<int> MyArrayList()
{
     return arrayList;
}

This way you can only read the elements in the next page. If you want to add any elements then you can use your Add() method.

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