473,734 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ***
Oct 31 '08 #1
4 3144
"Mike P" <mi*******@gmai l.comwrote in message
news:Oa******** ******@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.
Oct 31 '08 #2


"Mike P" <mi*******@gmai l.comwrote in message
news:Oa******** ******@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
Oct 31 '08 #3
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
Nov 3 '08 #4


"Marc Gravell" <ma**********@g mail.comwrote in message
news:Ol******** ******@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
Nov 3 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2015
by: Eddie | last post by:
Hi, I've recently read the articles on generics in the .net framework (the next version) and just wanted to check a few points.. As I understand, generic class instances while being strongly typed checked at compile time, are only created at runtime?? Is this correct??
27
2460
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and still no can do... Any info would be great!
9
3029
by: Chuck Bowling | last post by:
I assume that 2.0 will be rolled out with VS.NET 2004. Does anybody know if MS is planning to ship an STL package with it?
5
1196
by: A.M | last post by:
Hi, How can I store a class (including all it's members) in session object? I already tried something like: Session = myClass; but it stores the value of myClass.ToString in it!!
23
2547
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics vs C++ templates. Does anyone knows a workaround to do this ? Thx : public class C<T> { private T myValue;
12
2740
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as it defines the language more completely. Great to use.
9
5985
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal Experience Only, Please. ...
13
3831
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The problem I'm running into has to do with the differences in implementations of Generics between the two...
2
3604
by: NamelessNumberheadMan | last post by:
So here is my problem. I have two beans (one extends the other), and I have one DAO class for each to access their tables (using Hibernate). In most cases the queries will be identical so I also have an abstract DAO class (extended by the other two DAOs) which contains the majority of the queries. I had the methods returning beans of the super class which, if need be, I would cast to the subclass. To avoid this casting I want to switch to...
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.