简体   繁体   中英

I want to get the session name parameter by knowing its position c#

If I cycle all the session vars with a for I'll know each position and value of the session var but I want to know the session var name.

for ( int i=0; i<Session.count; i++) {
Response.write(Session[i].ToString());
}

But I want to retrieve Session var by Name like

Response.write(Session["SessionVarName"].ToString()); 

Use the Keys property of Session .

for ( int i=0; i < Session.Keys.Count; i++) {
    Response.write(Session.Keys[i]);
}

You can get the name of your session parameter by using Session.Keys[Index] , the value by index Session[Index] or value by parameter name Session["Name"] .

These are collection so you can loop over them to get all values and names.

foreach (string sVar in Session.Keys)
{
    // sVar contains the NAME of the key
}

or

for (i=0; i<Session.Count;i++)
    // Session.Keys[i] contains key name

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