Putting generics into session

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    Putting generics into session

    I have created my own generic class that I need to persist across
    postbacks. Here is my code :

    private void AgentSelector()
    {
    List<Agentagent List = new List<Agent>();

    CheckBox chk;
    int a = 0;

    foreach (GridViewRow rowItem in GridView1.Rows)
    {
    chk =
    (CheckBox)(rowI tem.Cells[0].FindControl("A gentSelector")) ;

    if (chk.Checked)
    {
    Agent agent = new Agent();

    agent.AgentID =
    Convert.ToInt32 (rowItem.Cells[1].Text);
    agent.AgentName =
    Convert.ToStrin g(rowItem.Cells[2].Text);

    agentList.Add(a gent);

    a++;
    }
    }

    GridView2.DataS ource = agentList;
    GridView2.DataB ind();

    Session["AgentList"] = agentList;

    pnlConfirm.Visi ble = false;
    pnlAgentSelecte d.Visible = true;
    }

    And then I am taking the list out of session in another method :

    protected void btnInsertAgent_ Click(object sender, EventArgs e)
    {
    List<Agentagent List = (List<Agent>)Se ssion["AgentList"];
    }


    Is this the right way to use generics? I am just wondering as I have
    never seen anyone else use generics like this.



    *** Sent via Developersdex http://www.developersdex.com ***
  • Alberto Poblacion

    #2
    Re: Putting generics into session

    "Mike P" <mike.parr@gmai l.comwrote in message
    news:OaB8Rn2OJH A.2348@TK2MSFTN GP05.phx.gbl...
    [...]
    Session["AgentList"] = agentList;
    [...]
    List<Agentagent List = (List<Agent>)Se ssion["AgentList"];
    >
    Is this the right way to use generics? I am just wondering as I have
    never seen anyone else use generics like this.
    This is fine. Generics can be put in the Session just like any
    non-generic type.
    I suggest that you mark your Agent class as [Serializable] in case you
    ever want to reconfigure the sessionState to be stored out-of-process.


    Comment

    • Mythran

      #3
      Re: Putting generics into session



      "Mike P" <mike.parr@gmai l.comwrote in message
      news:OaB8Rn2OJH A.2348@TK2MSFTN GP05.phx.gbl...
      I have created my own generic class that I need to persist across
      postbacks. Here is my code :
      >
      <snip>

      If you want the Agent list to be persisted across postbacks, mark it as
      Serializable and use the ViewState. This way, they aren't hanging around
      in-memory on the server when they navigate away from the page. If you need
      them persisted across pages, you can store them in Session for that, as you
      are already doing...but as Alberto has already stated in his/her/it's reply,
      you should still mark Agent as Serializable and add any additional
      Serialization code required (ISerializable implementation, if required).

      HTH,
      Mythran


      Comment

      • Marc Gravell

        #4
        Re: Putting generics into session

        Mythran: to be fair - you could et the same simply by using a
        database-based session provider; that way, the data doesn't keep going
        up/down the wire to the client.

        Mike: when used as viewstate (or with a databased-base session
        provider), the data must be serialized to flat bytes to be stored; hence
        the data needs to be [Serializeable] (automatic) or ISerializable (manual).

        Marc

        Comment

        • Mythran

          #5
          Re: Putting generics into session



          "Marc Gravell" <marc.gravell@g mail.comwrote in message
          news:Olp0ZsZPJH A.4680@TK2MSFTN GP06.phx.gbl...
          Mythran: to be fair - you could et the same simply by using a
          database-based session provider; that way, the data doesn't keep going
          up/down the wire to the client.
          >
          Mike: when used as viewstate (or with a databased-base session provider),
          the data must be serialized to flat bytes to be stored; hence the data
          needs to be [Serializeable] (automatic) or ISerializable (manual).
          >
          Marc
          Aye Marc, but using a database-based session provide means you are still
          using Session (just with a different backing store). I didn't want to get
          into all the different options for Session or cookies or other possibilies.
          Just "Session" or "ViewState" . But, if there is quite a bit of data being
          passed back and forth, you are correct, I would use a better backing store
          (such as a db).

          Mythran


          Comment

          Working...