locked
Using arrayList as DataSource for Gridview RRS feed

  • Question

  • User-2037392709 posted
    Hi, Im trying to implement this way of populating a gridview https://aspdotnetcodebook.blogspot.com.ar/2008/07/using-arraylist-as-datasource-for.html but using 2 layer architecture (UI - BO).
    BO has 2 class (Person.cs and PersonList.cs)
    I want to move the method that create/retrieve the Session to the PersonList.cs and call it from the UI (Default.aspx.cs in the page load). Basically the same as in the page linked but using a layer for the objects (Person and PersonList which retrieves a Session Object populated with a List of Person?). 
    Friday, April 20, 2018 1:19 PM

All replies

  • User753101303 posted

    Hi,

    Just replacing ViewData with Session doesn't work? Please be more explicit about the problem you have when doing that.

    Not directly related but if you start, I  would suggest to have a look at https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data series which take advantage of the latest updates (also it is relatively similar to MVC)...

    Friday, April 20, 2018 1:37 PM
  • User-2037392709 posted

    Sorry for my english, I will try to explain it better.

    Lets say I have a class library and proyect (empty webform asp.net). In the class library I have Person.cs and PersonList.cs

    Person.cs

    [Serializable()]
    class Person
    {
        public string name { get; set; }
        public string surname { get; set; }
    }
    
    PersonList.cs
    class PersonList { public List<Person> SessionData //Should retrieve the Session as List. { get { if (HttpContext.Current.Session["List<Person>"] == null) { HttpContext.Current.Session["List<Person>"] = new List<Person>(); } return HttpContext.Current.Session["List<Person>"] as List<Person>; } } }

    Now from a simple webform (Default.aspx.cs) I want to retrieve the Session and also be able to Add items to that List.

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
              //How to retrieve the Session?
    } } protected void BtnArgegar_Click(object sender, EventArgs e) { Person per = new Person(); Person.name = "Test"; Person.surname = "Test"; //Retrieve Session again and add the new item //bind it to the gridview
    this.Bind() }

    private void Bind()
    {
    Grid.DataSource = //Retrieve Session;
    Grid.DataBind();
    }

    I wanted to use a class library to separate the objects and methods from the webpage that is being used, like a 2 layer architecture? (ui - bo)

    Friday, April 20, 2018 1:55 PM
  • User475983607 posted

    I wanted to use a class library to separate the objects and methods from the webpage that is being used, like a 2 layer architecture? (ui - bo)

    Session is only available during the page life cycle.  The way the code is currently written, you must new up a SessionData type in every class that uses it.  It's far less code to simply access Session directly in the page class.   Craft a static class If want to take advantage of intellisense rather than remembering string indexes across web pages.

    I'm not a fan of your design approach as it does not scale out.  Plus the code becomes overly complex especially if the List<Person> is used on a handful of pages.  Every page that touches List<Person> can be out of sync with a database.  

    The standard pattern used with Session is a cache that expires relatively quickly or at a rate consistent with how you expect the List<Person> to change.

    if (Session["List<Person>"] == null)
    {
        //Fill Session
    }
    else
    {
        //return Session
    }

    Friday, April 20, 2018 5:16 PM
  • User-2037392709 posted

    The approach from the page linked its good enough then I guess. Could you explain me about crafting static class? The purpose is to make easier the debug?

    This is how it ended in the code: 

    [Serializable()]
    public class Person
    {
    private string name;
    private string surname;

    }

    And the webpage like this:

    public partial class Default : System.Web.UI.UserControl
    {

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!this.IsPostBack)
    {
    this.GetData();
    }
    }

    private void GetData()
    {
    GridPerson.DataSource = this.Data;
    GridPerson.DataBind();
    }

    private List<Equipo> Data
    {
    get
    {
    if (this.Session["Data"] == null)
    {
    this.Session["Data"] = new List<Person>();
    }
    return this.Session["Data"] as List<Person>;
    }
    }

    protected void BtnArgegar_Click(object sender, EventArgs e) {

    Person per = new Person();

    createPerson(per);

    this.Data.Add(per);

    this.GetData();

    }

    private void createPerson(Person per) {

    per.name = "Austin";

    per.surname = "Power";

    }

    Friday, April 20, 2018 6:41 PM