Putting generics into session

M

Mike P

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

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

CheckBox chk;
int a = 0;

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

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

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

agentList.Add(agent);

a++;
}
}

GridView2.DataSource = agentList;
GridView2.DataBind();

Session["AgentList"] = agentList;

pnlConfirm.Visible = false;
pnlAgentSelected.Visible = true;
}

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

protected void btnInsertAgent_Click(object sender, EventArgs e)
{
List<Agent> agentList = (List<Agent>)Session["AgentList"];
}


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

Alberto Poblacion

Mike P said:
[...]
Session["AgentList"] = agentList;
[...]
List<Agent> agentList = (List<Agent>)Session["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.
 
M

Mythran

Mike P said:
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
 
M

Mike P

Why do you need to mark the class as Serializable in order to use
ViewState instead of Session?
 
M

Marc Gravell

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
 
M

Mythran

Marc Gravell said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top