我想知道它的位置c#获取会话名称参数

时间:2011-12-14 08:29:28

标签: c# session

如果我使用for循环所有会话变量,我将知道会话变量的每个位置和值,但我想知道会话变量名称。

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

但我想通过名称检索Session var,如

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

3 个答案:

答案 0 :(得分:4)

使用Session的{​​{3}}属性。

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

答案 1 :(得分:0)

您可以使用Session.Keys[Index]获取会话参数的名称,使用索引Session[Index]获取值或使用参数名称Session["Name"]获取值。

这些是集合,因此您可以循环遍历它们以获取所有值和名称。

答案 2 :(得分:0)

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

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